Skip to content

Commit 5b84966

Browse files
committed
Auto merge of #3640 - detrumi:nested_use_self, r=flip1995
Restrict `use_self` on nested items Fixes #3637 Fixes #3463 These changes make it so that nested items aren't visited any more by the `use_self` lint. I think visiting nested items should be possible (so that it uses a different `item_path` for the nested item), but I'm not sure whether it's viable and what the best approach would be. - Can `item_path` be changed to a new `Self` path before visiting the item, and then changing it back afterwards? - Alternatively, could a new visitor be created, re-using `check_trait_method_impl_decl`?
2 parents 140c165 + 466cd07 commit 5b84966

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

Diff for: clippy_lints/src/use_self.rs

+13-5
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
@@ -29,7 +28,6 @@ use syntax_pos::symbol::keywords::SelfUpper;
2928
/// **Known problems:**
3029
/// - False positive when using associated types (#2843)
3130
/// - False positives in some situations when using generics (#3410)
32-
/// - False positive when type from outer function can't be used (#3463)
3331
///
3432
/// **Example:**
3533
/// ```rust
@@ -242,8 +240,18 @@ impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
242240
walk_path(self, path);
243241
}
244242

245-
fn visit_use(&mut self, _path: &'tcx Path, _id: NodeId, _hir_id: HirId) {
246-
// Don't check use statements
243+
fn visit_item(&mut self, item: &'tcx Item) {
244+
match item.node {
245+
ItemKind::Use(..)
246+
| ItemKind::Static(..)
247+
| ItemKind::Enum(..)
248+
| ItemKind::Struct(..)
249+
| ItemKind::Union(..)
250+
| ItemKind::Impl(..) => {
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> {

Diff for: tests/ui/use_self.rs

+28-11
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,34 @@ 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+
impl Bar {
255+
fn bar() -> Bar {
256+
Bar { foo: Foo {} }
257+
}
258+
}
259+
}
260+
}
261+
262+
enum Enum {
263+
A,
264+
}
265+
impl Enum {
266+
fn method() {
267+
use self::Enum::*; // Issue 3425
268+
static STATIC: Enum = Enum::A; // Can't use Self as type
269+
}
270+
}
271+
}
272+
245273
mod issue3410 {
246274

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

Diff for: tests/ui/use_self.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,17 @@ LL | Foo {}
150150
LL | use_self_expand!(); // Should lint in local macros
151151
| ------------------- in this macro invocation
152152

153-
error: aborting due to 24 previous errors
153+
error: unnecessary structure name repetition
154+
--> $DIR/use_self.rs:255:29
155+
|
156+
LL | fn bar() -> Bar {
157+
| ^^^ help: use the applicable keyword: `Self`
158+
159+
error: unnecessary structure name repetition
160+
--> $DIR/use_self.rs:256:21
161+
|
162+
LL | Bar { foo: Foo {} }
163+
| ^^^ help: use the applicable keyword: `Self`
164+
165+
error: aborting due to 26 previous errors
154166

0 commit comments

Comments
 (0)