Skip to content

Commit 7bbea69

Browse files
committed
support unit enum variants
1 parent 6906c68 commit 7bbea69

File tree

4 files changed

+45
-27
lines changed

4 files changed

+45
-27
lines changed

clippy_lints/src/use_self.rs

+33-15
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
1616
use rustc_span::{BytePos, Span};
1717
use rustc_typeck::hir_ty_to_ty;
1818

19-
use crate::utils::{differing_macro_contexts, span_lint_and_sugg};
19+
use crate::utils::span_lint_and_sugg;
2020

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

82+
fn span_lint_on_qpath_resolved<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, qpath: &'tcx QPath<'tcx>, enum_variant: bool) {
83+
match qpath {
84+
QPath::Resolved(_, path) => {
85+
if enum_variant {
86+
span_lint_ignore_last_segment(cx, path);
87+
} else {
88+
span_lint(cx, path.span);
89+
}
90+
},
91+
_ => (),
92+
};
93+
}
94+
8295
struct ImplVisitor<'a, 'tcx> {
8396
cx: &'a LateContext<'a, 'tcx>,
8497
item: &'tcx Item<'tcx>,
@@ -185,29 +198,34 @@ impl<'a, 'tcx> Visitor<'tcx> for ImplVisitor<'a, 'tcx> {
185198
}
186199
}
187200
},
201+
// type-relative fn calls (`Foo::new()`) and tuple-like instantiation (`Foo(arg)` or `Enum::Foo(arg)`)
188202
ExprKind::Call(
189-
Expr {
190-
kind:
191-
ExprKind::Path(QPath::Resolved(
192-
_,
193-
path
194-
@
195-
Path {
196-
res: def::Res::Def(def::DefKind::Ctor(ctor_of, _), _),
197-
..
198-
},
199-
)),
203+
fun
204+
@ Expr {
205+
kind: ExprKind::Path(ref qpath),
200206
..
201207
},
202208
_,
203209
) => {
204210
if expr_ty_matches(expr, self.self_ty, self.cx) {
205-
match ctor_of {
206-
def::CtorOf::Struct => span_lint(self.cx, path.span),
207-
def::CtorOf::Variant => span_lint_ignore_last_segment(self.cx, path),
211+
let res = self.cx.tables.qpath_res(qpath, fun.hir_id);
212+
213+
if let def::Res::Def(DefKind::Ctor(ctor_of, _), ..) = res {
214+
match ctor_of {
215+
def::CtorOf::Variant => span_lint_on_qpath_resolved(self.cx, qpath, true),
216+
def::CtorOf::Struct => span_lint_on_qpath_resolved(self.cx, qpath, false),
217+
}
218+
} else if let def::Res::Def(DefKind::Variant, ..) = res {
219+
span_lint_on_qpath_resolved(self.cx, qpath, true);
208220
}
209221
}
210222
},
223+
// unit enum variants (`Enum::A`)
224+
ExprKind::Path(ref qpath) => {
225+
if expr_ty_matches(expr, self.self_ty, self.cx) {
226+
span_lint_on_qpath_resolved(self.cx, qpath, true);
227+
}
228+
},
211229
_ => (),
212230
}
213231
walk_expr(self, expr);

tests/ui/use_self.fixed

+4-4
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ mod nesting {
167167
let _ = Self::B(42);
168168
let _ = Self::C { field: true };
169169
// TODO: support unit struct
170-
let _ = Enum::A;
170+
let _ = Self::A;
171171
}
172172
}
173173
}
@@ -249,9 +249,9 @@ mod paths_created_by_lowering {
249249
const A: usize = 0;
250250
const B: usize = 1;
251251

252-
async fn g() -> S {
253-
Self {}
254-
}
252+
/* async fn g() -> S {
253+
S {}
254+
} */
255255

256256
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
257257
&p[Self::A..Self::B]

tests/ui/use_self.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ mod paths_created_by_lowering {
249249
const A: usize = 0;
250250
const B: usize = 1;
251251

252-
async fn g() -> S {
252+
/* async fn g() -> S {
253253
S {}
254-
}
254+
} */
255255

256256
fn f<'a>(&self, p: &'a [u8]) -> &'a [u8] {
257257
&p[S::A..S::B]

tests/ui/use_self.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ error: unnecessary structure name repetition
100100
LL | let _ = Enum::C { field: true };
101101
| ^^^^ help: use the applicable keyword: `Self`
102102

103+
error: unnecessary structure name repetition
104+
--> $DIR/use_self.rs:170:21
105+
|
106+
LL | let _ = Enum::A;
107+
| ^^^^ help: use the applicable keyword: `Self`
108+
103109
error: unnecessary structure name repetition
104110
--> $DIR/use_self.rs:216:13
105111
|
@@ -124,12 +130,6 @@ error: unnecessary structure name repetition
124130
LL | TestStruct::from_something()
125131
| ^^^^^^^^^^ help: use the applicable keyword: `Self`
126132

127-
error: unnecessary structure name repetition
128-
--> $DIR/use_self.rs:253:13
129-
|
130-
LL | S {}
131-
| ^ help: use the applicable keyword: `Self`
132-
133133
error: unnecessary structure name repetition
134134
--> $DIR/use_self.rs:257:16
135135
|

0 commit comments

Comments
 (0)