Skip to content

Commit ff191a8

Browse files
committed
Restrict use_self on nested items
1 parent c63b634 commit ff191a8

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

clippy_lints/src/use_self.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
use crate::utils::span_lint_and_sugg;
1111
use if_chain::if_chain;
1212
use rustc::hir::def::{CtorKind, Def};
13-
use rustc::hir::intravisit::{walk_path, walk_ty, NestedVisitorMap, Visitor};
13+
use rustc::hir::intravisit::{walk_item, walk_path, walk_ty, NestedVisitorMap, Visitor};
1414
use rustc::hir::*;
1515
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
1616
use rustc::ty;
1717
use rustc::{declare_tool_lint, lint_array};
1818
use rustc_errors::Applicability;
19-
use syntax::ast::NodeId;
2019
use syntax_pos::symbol::keywords::SelfUpper;
2120

2221
/// **What it does:** Checks for unnecessary repetition of structure name when a
@@ -242,8 +241,17 @@ impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
242241
walk_path(self, path);
243242
}
244243

245-
fn visit_use(&mut self, _path: &'tcx Path, _id: NodeId, _hir_id: HirId) {
246-
// Don't check use statements
244+
fn visit_item(&mut self, item: &'tcx Item) {
245+
match item.node {
246+
ItemKind::Use(..)
247+
| ItemKind::Static(..)
248+
| ItemKind::Enum(..)
249+
| ItemKind::Struct(..)
250+
| ItemKind::Union(..) => {
251+
// Don't check statements that shadow `Self` or where `Self` can't be used
252+
},
253+
_ => walk_item(self, item),
254+
}
247255
}
248256

249257
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {

tests/ui/use_self.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,28 @@ mod macros {
242242
}
243243
}
244244

245+
mod nesting {
246+
struct Foo {}
247+
impl Foo {
248+
fn foo() {
249+
use self::Foo; // Can't use Self here
250+
struct Bar {
251+
foo: Foo, // Foo != Self
252+
}
253+
}
254+
}
255+
256+
enum Enum {
257+
A,
258+
}
259+
impl Enum {
260+
fn method() {
261+
use self::Enum::*;
262+
static STATIC: Enum = Enum::A; // Can't use Self as type
263+
}
264+
}
265+
}
266+
245267
mod issue3410 {
246268

247269
struct A;
@@ -255,14 +277,3 @@ mod issue3410 {
255277
fn a(_: Vec<A>) {}
256278
}
257279
}
258-
259-
mod issue3425 {
260-
enum Enum {
261-
A,
262-
}
263-
impl Enum {
264-
fn a() {
265-
use self::Enum::*;
266-
}
267-
}
268-
}

0 commit comments

Comments
 (0)