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`?
This commit is contained in:
bors 2019-01-07 18:54:28 +00:00
commit 5b8496603c
3 changed files with 54 additions and 17 deletions

View file

@ -10,13 +10,12 @@
use crate::utils::span_lint_and_sugg;
use if_chain::if_chain;
use rustc::hir::def::{CtorKind, Def};
use rustc::hir::intravisit::{walk_path, walk_ty, NestedVisitorMap, Visitor};
use rustc::hir::intravisit::{walk_item, walk_path, walk_ty, NestedVisitorMap, Visitor};
use rustc::hir::*;
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
use rustc::ty;
use rustc::{declare_tool_lint, lint_array};
use rustc_errors::Applicability;
use syntax::ast::NodeId;
use syntax_pos::symbol::keywords::SelfUpper;
/// **What it does:** Checks for unnecessary repetition of structure name when a
@ -29,7 +28,6 @@ use syntax_pos::symbol::keywords::SelfUpper;
/// **Known problems:**
/// - False positive when using associated types (#2843)
/// - False positives in some situations when using generics (#3410)
/// - False positive when type from outer function can't be used (#3463)
///
/// **Example:**
/// ```rust
@ -242,8 +240,18 @@ impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
walk_path(self, path);
}
fn visit_use(&mut self, _path: &'tcx Path, _id: NodeId, _hir_id: HirId) {
// Don't check use statements
fn visit_item(&mut self, item: &'tcx Item) {
match item.node {
ItemKind::Use(..)
| ItemKind::Static(..)
| ItemKind::Enum(..)
| ItemKind::Struct(..)
| ItemKind::Union(..)
| ItemKind::Impl(..) => {
// Don't check statements that shadow `Self` or where `Self` can't be used
},
_ => walk_item(self, item),
}
}
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {

View file

@ -242,6 +242,34 @@ mod macros {
}
}
mod nesting {
struct Foo {}
impl Foo {
fn foo() {
use self::Foo; // Can't use Self here
struct Bar {
foo: Foo, // Foo != Self
}
impl Bar {
fn bar() -> Bar {
Bar { foo: Foo {} }
}
}
}
}
enum Enum {
A,
}
impl Enum {
fn method() {
use self::Enum::*; // Issue 3425
static STATIC: Enum = Enum::A; // Can't use Self as type
}
}
}
mod issue3410 {
struct A;
@ -255,14 +283,3 @@ mod issue3410 {
fn a(_: Vec<A>) {}
}
}
mod issue3425 {
enum Enum {
A,
}
impl Enum {
fn a() {
use self::Enum::*;
}
}
}

View file

@ -150,5 +150,17 @@ LL | Foo {}
LL | use_self_expand!(); // Should lint in local macros
| ------------------- in this macro invocation
error: aborting due to 24 previous errors
error: unnecessary structure name repetition
--> $DIR/use_self.rs:255:29
|
LL | fn bar() -> Bar {
| ^^^ help: use the applicable keyword: `Self`
error: unnecessary structure name repetition
--> $DIR/use_self.rs:256:21
|
LL | Bar { foo: Foo {} }
| ^^^ help: use the applicable keyword: `Self`
error: aborting due to 26 previous errors