Skip to content

Restrict use_self on nested items #3640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions clippy_lints/src/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -242,8 +240,17 @@ 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(..) => {
// 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> {
Expand Down
33 changes: 22 additions & 11 deletions tests/ui/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,28 @@ mod macros {
}
}

mod nesting {
struct Foo {}
impl Foo {
fn foo() {
use self::Foo; // Can't use Self here
struct Bar {
foo: Foo, // Foo != Self
}
}
}

enum Enum {
A,
}
impl Enum {
fn method() {
use self::Enum::*;
static STATIC: Enum = Enum::A; // Can't use Self as type
}
}
}

mod issue3410 {

struct A;
Expand All @@ -255,14 +277,3 @@ mod issue3410 {
fn a(_: Vec<A>) {}
}
}

mod issue3425 {
enum Enum {
A,
}
impl Enum {
fn a() {
use self::Enum::*;
}
}
}