Skip to content

Commit

Permalink
support unit enum variants
Browse files Browse the repository at this point in the history
  • Loading branch information
tnielens committed Apr 26, 2020
1 parent 6906c68 commit 7bbea69
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 27 deletions.
48 changes: 33 additions & 15 deletions clippy_lints/src/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{BytePos, Span};
use rustc_typeck::hir_ty_to_ty;

use crate::utils::{differing_macro_contexts, span_lint_and_sugg};
use crate::utils::span_lint_and_sugg;

declare_clippy_lint! {
/// **What it does:** Checks for unnecessary repetition of structure name when a
Expand Down Expand Up @@ -79,6 +79,19 @@ fn span_lint_ignore_last_segment<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, path: &'t
}
}

fn span_lint_on_qpath_resolved<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, qpath: &'tcx QPath<'tcx>, enum_variant: bool) {
match qpath {
QPath::Resolved(_, path) => {
if enum_variant {
span_lint_ignore_last_segment(cx, path);
} else {
span_lint(cx, path.span);
}
},
_ => (),
};
}

struct ImplVisitor<'a, 'tcx> {
cx: &'a LateContext<'a, 'tcx>,
item: &'tcx Item<'tcx>,
Expand Down Expand Up @@ -185,29 +198,34 @@ impl<'a, 'tcx> Visitor<'tcx> for ImplVisitor<'a, 'tcx> {
}
}
},
// type-relative fn calls (`Foo::new()`) and tuple-like instantiation (`Foo(arg)` or `Enum::Foo(arg)`)
ExprKind::Call(
Expr {
kind:
ExprKind::Path(QPath::Resolved(
_,
path
@
Path {
res: def::Res::Def(def::DefKind::Ctor(ctor_of, _), _),
..
},
)),
fun
@ Expr {
kind: ExprKind::Path(ref qpath),
..
},
_,
) => {
if expr_ty_matches(expr, self.self_ty, self.cx) {
match ctor_of {
def::CtorOf::Struct => span_lint(self.cx, path.span),
def::CtorOf::Variant => span_lint_ignore_last_segment(self.cx, path),
let res = self.cx.tables.qpath_res(qpath, fun.hir_id);

if let def::Res::Def(DefKind::Ctor(ctor_of, _), ..) = res {
match ctor_of {
def::CtorOf::Variant => span_lint_on_qpath_resolved(self.cx, qpath, true),
def::CtorOf::Struct => span_lint_on_qpath_resolved(self.cx, qpath, false),
}
} else if let def::Res::Def(DefKind::Variant, ..) = res {
span_lint_on_qpath_resolved(self.cx, qpath, true);
}
}
},
// unit enum variants (`Enum::A`)
ExprKind::Path(ref qpath) => {
if expr_ty_matches(expr, self.self_ty, self.cx) {
span_lint_on_qpath_resolved(self.cx, qpath, true);
}
},
_ => (),
}
walk_expr(self, expr);
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/use_self.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ mod nesting {
let _ = Self::B(42);
let _ = Self::C { field: true };
// TODO: support unit struct
let _ = Enum::A;
let _ = Self::A;
}
}
}
Expand Down Expand Up @@ -249,9 +249,9 @@ mod paths_created_by_lowering {
const A: usize = 0;
const B: usize = 1;

async fn g() -> S {
Self {}
}
/* async fn g() -> S {
S {}
} */

fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
&p[Self::A..Self::B]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ mod paths_created_by_lowering {
const A: usize = 0;
const B: usize = 1;

async fn g() -> S {
/* async fn g() -> S {
S {}
}
} */

fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
&p[S::A..S::B]
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/use_self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ error: unnecessary structure name repetition
LL | let _ = Enum::C { field: true };
| ^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:170:21
|
LL | let _ = Enum::A;
| ^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:216:13
|
Expand All @@ -124,12 +130,6 @@ error: unnecessary structure name repetition
LL | TestStruct::from_something()
| ^^^^^^^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:253:13
|
LL | S {}
| ^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> $DIR/use_self.rs:257:16
|
Expand Down

0 comments on commit 7bbea69

Please sign in to comment.