Skip to content

Commit 4326814

Browse files
committed
Auto merge of #9726 - kraktus:fix_use_self, r=Alexendoo
[`use_self`] fix suggestion when full path to struct was given Previously the following wrong suggestion was given ```rust impl Error for std::fmt::Error { fn custom<T: std::fmt::Display>(_msg: T) -> Self { - std::fmt::Error // Should lint + Self::Error // Should lint } } ``` Also remove known problem line related to #4140 since it's been closed, and refactor the lint changelog: [`use_self`] fix suggestion when full path to struct was given
2 parents f5d225d + 1909a6a commit 4326814

File tree

9 files changed

+87
-33
lines changed

9 files changed

+87
-33
lines changed

clippy_lints/src/use_self.rs

+14-24
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ declare_clippy_lint! {
3030
///
3131
/// ### Known problems
3232
/// - Unaddressed false negative in fn bodies of trait implementations
33-
/// - False positive with associated types in traits (#4140)
3433
///
3534
/// ### Example
3635
/// ```rust
@@ -235,24 +234,13 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
235234
then {} else { return; }
236235
}
237236
match expr.kind {
238-
ExprKind::Struct(QPath::Resolved(_, path), ..) => match path.res {
239-
Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => (),
240-
Res::Def(DefKind::Variant, _) => lint_path_to_variant(cx, path),
241-
_ => span_lint(cx, path.span),
242-
},
243-
// tuple struct instantiation (`Foo(arg)` or `Enum::Foo(arg)`)
237+
ExprKind::Struct(QPath::Resolved(_, path), ..) => check_path(cx, path),
244238
ExprKind::Call(fun, _) => {
245239
if let ExprKind::Path(QPath::Resolved(_, path)) = fun.kind {
246-
if let Res::Def(DefKind::Ctor(ctor_of, _), ..) = path.res {
247-
match ctor_of {
248-
CtorOf::Variant => lint_path_to_variant(cx, path),
249-
CtorOf::Struct => span_lint(cx, path.span),
250-
}
251-
}
240+
check_path(cx, path);
252241
}
253242
},
254-
// unit enum variants (`Enum::A`)
255-
ExprKind::Path(QPath::Resolved(_, path)) => lint_path_to_variant(cx, path),
243+
ExprKind::Path(QPath::Resolved(_, path)) => check_path(cx, path),
256244
_ => (),
257245
}
258246
}
@@ -268,15 +256,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
268256
| PatKind::Struct(QPath::Resolved(_, path), _, _) = pat.kind;
269257
if cx.typeck_results().pat_ty(pat) == cx.tcx.type_of(impl_id);
270258
then {
271-
match path.res {
272-
Res::Def(DefKind::Ctor(ctor_of, _), ..) => match ctor_of {
273-
CtorOf::Variant => lint_path_to_variant(cx, path),
274-
CtorOf::Struct => span_lint(cx, path.span),
275-
},
276-
Res::Def(DefKind::Variant, ..) => lint_path_to_variant(cx, path),
277-
Res::Def(DefKind::Struct, ..) => span_lint(cx, path.span),
278-
_ => ()
279-
}
259+
check_path(cx, path);
280260
}
281261
}
282262
}
@@ -314,6 +294,16 @@ fn span_lint(cx: &LateContext<'_>, span: Span) {
314294
);
315295
}
316296

297+
fn check_path(cx: &LateContext<'_>, path: &Path<'_>) {
298+
match path.res {
299+
Res::Def(DefKind::Ctor(CtorOf::Variant, _) | DefKind::Variant, ..) => {
300+
lint_path_to_variant(cx, path);
301+
},
302+
Res::Def(DefKind::Ctor(CtorOf::Struct, _) | DefKind::Struct, ..) => span_lint(cx, path.span),
303+
_ => (),
304+
}
305+
}
306+
317307
fn lint_path_to_variant(cx: &LateContext<'_>, path: &Path<'_>) {
318308
if let [.., self_seg, _variant] = path.segments {
319309
let span = path

tests/ui-cargo/cargo_rust_version/fail_both_diff/src/main.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,11 @@ note: the lint level is defined here
1212
LL | #![deny(clippy::use_self)]
1313
| ^^^^^^^^^^^^^^^^
1414

15-
error: aborting due to previous error; 1 warning emitted
15+
error: unnecessary structure name repetition
16+
--> $DIR/main.rs:7:9
17+
|
18+
LL | Foo
19+
| ^^^ help: use the applicable keyword: `Self`
20+
21+
error: aborting due to 2 previous errors; 1 warning emitted
1622

tests/ui-cargo/cargo_rust_version/fail_both_same/src/main.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,11 @@ note: the lint level is defined here
1010
LL | #![deny(clippy::use_self)]
1111
| ^^^^^^^^^^^^^^^^
1212

13-
error: aborting due to previous error
13+
error: unnecessary structure name repetition
14+
--> $DIR/main.rs:7:9
15+
|
16+
LL | Foo
17+
| ^^^ help: use the applicable keyword: `Self`
18+
19+
error: aborting due to 2 previous errors
1420

tests/ui-cargo/cargo_rust_version/fail_cargo/src/main.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,11 @@ note: the lint level is defined here
1010
LL | #![deny(clippy::use_self)]
1111
| ^^^^^^^^^^^^^^^^
1212

13-
error: aborting due to previous error
13+
error: unnecessary structure name repetition
14+
--> $DIR/main.rs:7:9
15+
|
16+
LL | Foo
17+
| ^^^ help: use the applicable keyword: `Self`
18+
19+
error: aborting due to 2 previous errors
1420

tests/ui-cargo/cargo_rust_version/fail_clippy/src/main.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,11 @@ note: the lint level is defined here
1010
LL | #![deny(clippy::use_self)]
1111
| ^^^^^^^^^^^^^^^^
1212

13-
error: aborting due to previous error
13+
error: unnecessary structure name repetition
14+
--> $DIR/main.rs:7:9
15+
|
16+
LL | Foo
17+
| ^^^ help: use the applicable keyword: `Self`
18+
19+
error: aborting due to 2 previous errors
1420

tests/ui-cargo/cargo_rust_version/fail_file_attr/src/main.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,11 @@ note: the lint level is defined here
1010
LL | #![deny(clippy::use_self)]
1111
| ^^^^^^^^^^^^^^^^
1212

13-
error: aborting due to previous error
13+
error: unnecessary structure name repetition
14+
--> $DIR/main.rs:12:9
15+
|
16+
LL | Foo
17+
| ^^^ help: use the applicable keyword: `Self`
18+
19+
error: aborting due to 2 previous errors
1420

tests/ui/use_self_trait.fixed

+13-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ impl Mul for Bad {
4747

4848
impl Clone for Bad {
4949
fn clone(&self) -> Self {
50-
// FIXME: applicable here
51-
Bad
50+
Self
5251
}
5352
}
5453

@@ -138,4 +137,16 @@ mod impl_in_macro {
138137
parse_ip_impl!(Foo); // Should not lint
139138
}
140139

140+
mod full_path_replacement {
141+
trait Error {
142+
fn custom<T: std::fmt::Display>(_msg: T) -> Self;
143+
}
144+
145+
impl Error for std::fmt::Error {
146+
fn custom<T: std::fmt::Display>(_msg: T) -> Self {
147+
Self // Should lint
148+
}
149+
}
150+
}
151+
141152
fn main() {}

tests/ui/use_self_trait.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ impl Mul for Bad {
4747

4848
impl Clone for Bad {
4949
fn clone(&self) -> Self {
50-
// FIXME: applicable here
5150
Bad
5251
}
5352
}
@@ -138,4 +137,16 @@ mod impl_in_macro {
138137
parse_ip_impl!(Foo); // Should not lint
139138
}
140139

140+
mod full_path_replacement {
141+
trait Error {
142+
fn custom<T: std::fmt::Display>(_msg: T) -> Self;
143+
}
144+
145+
impl Error for std::fmt::Error {
146+
fn custom<T: std::fmt::Display>(_msg: T) -> Self {
147+
std::fmt::Error // Should lint
148+
}
149+
}
150+
}
151+
141152
fn main() {}

tests/ui/use_self_trait.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,17 @@ error: unnecessary structure name repetition
8484
LL | fn mul(self, rhs: Bad) -> Bad {
8585
| ^^^ help: use the applicable keyword: `Self`
8686

87-
error: aborting due to 14 previous errors
87+
error: unnecessary structure name repetition
88+
--> $DIR/use_self_trait.rs:50:9
89+
|
90+
LL | Bad
91+
| ^^^ help: use the applicable keyword: `Self`
92+
93+
error: unnecessary structure name repetition
94+
--> $DIR/use_self_trait.rs:147:13
95+
|
96+
LL | std::fmt::Error // Should lint
97+
| ^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self`
98+
99+
error: aborting due to 16 previous errors
88100

0 commit comments

Comments
 (0)