Skip to content

Commit a77e881

Browse files
committed
should_impl_trait - ignore methods with lifetime params
1 parent 70c46de commit a77e881

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1497,11 +1497,20 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
14971497
if cx.access_levels.is_exported(impl_item.hir_id) {
14981498
// check missing trait implementations
14991499
for &(method_name, n_args, fn_header, self_kind, out_type, trait_name) in &TRAIT_METHODS {
1500+
let no_lifetime_params = || {
1501+
impl_item.generics.params.iter().filter(|p| match p.kind {
1502+
hir::GenericParamKind::Lifetime { .. } => true,
1503+
_ => false,
1504+
}).count() == 0
1505+
};
15001506
if name == method_name &&
15011507
sig.decl.inputs.len() == n_args &&
15021508
out_type.matches(cx, &sig.decl.output) &&
15031509
self_kind.matches(cx, self_ty, first_arg_ty) &&
1504-
fn_header_equals(*fn_header, sig.header) {
1510+
fn_header_equals(*fn_header, sig.header) &&
1511+
// ignore methods with lifetime params, risk of false positive
1512+
no_lifetime_params()
1513+
{
15051514
span_lint(cx, SHOULD_IMPLEMENT_TRAIT, impl_item.span, &format!(
15061515
"defining a method called `{}` on this type; consider implementing \
15071516
the `{}` trait or choosing a less ambiguous name", name, trait_name));

tests/ui/methods.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
clippy::non_ascii_literal,
1111
clippy::new_without_default,
1212
clippy::needless_pass_by_value,
13+
clippy::needless_lifetimes,
1314
clippy::print_stdout,
1415
clippy::must_use_candidate,
1516
clippy::use_self,
@@ -82,6 +83,10 @@ impl T {
8283
fn new(self) -> Self {
8384
unimplemented!();
8485
}
86+
87+
pub fn next<'b>(&'b mut self) -> Option<&'b mut T> {
88+
unimplemented!();
89+
}
8590
}
8691

8792
pub struct T1;

tests/ui/methods.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: defining a method called `add` on this type; consider implementing the `std::ops::Add` trait or choosing a less ambiguous name
2-
--> $DIR/methods.rs:39:5
2+
--> $DIR/methods.rs:40:5
33
|
44
LL | / pub fn add(self, other: T) -> T {
55
LL | | self
@@ -9,7 +9,7 @@ LL | | }
99
= note: `-D clippy::should-implement-trait` implied by `-D warnings`
1010

1111
error: methods called `new` usually return `Self`
12-
--> $DIR/methods.rs:169:5
12+
--> $DIR/methods.rs:174:5
1313
|
1414
LL | / fn new() -> i32 {
1515
LL | | 0
@@ -19,7 +19,7 @@ LL | | }
1919
= note: `-D clippy::new-ret-no-self` implied by `-D warnings`
2020

2121
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
22-
--> $DIR/methods.rs:188:13
22+
--> $DIR/methods.rs:193:13
2323
|
2424
LL | let _ = v.iter().filter(|&x| *x < 0).next();
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -28,7 +28,7 @@ LL | let _ = v.iter().filter(|&x| *x < 0).next();
2828
= note: replace `filter(|&x| *x < 0).next()` with `find(|&x| *x < 0)`
2929

3030
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
31-
--> $DIR/methods.rs:191:13
31+
--> $DIR/methods.rs:196:13
3232
|
3333
LL | let _ = v.iter().filter(|&x| {
3434
| _____________^
@@ -38,33 +38,33 @@ LL | | ).next();
3838
| |___________________________^
3939

4040
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
41-
--> $DIR/methods.rs:208:22
41+
--> $DIR/methods.rs:213:22
4242
|
4343
LL | let _ = v.iter().find(|&x| *x < 0).is_some();
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `any(|x| *x < 0)`
4545
|
4646
= note: `-D clippy::search-is-some` implied by `-D warnings`
4747

4848
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
49-
--> $DIR/methods.rs:209:20
49+
--> $DIR/methods.rs:214:20
5050
|
5151
LL | let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less
5252
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `any(|x| **y == x)`
5353

5454
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
55-
--> $DIR/methods.rs:210:20
55+
--> $DIR/methods.rs:215:20
5656
|
5757
LL | let _ = (0..1).find(|x| *x == 0).is_some();
5858
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `any(|x| x == 0)`
5959

6060
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
61-
--> $DIR/methods.rs:211:22
61+
--> $DIR/methods.rs:216:22
6262
|
6363
LL | let _ = v.iter().find(|x| **x == 0).is_some();
6464
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `any(|x| *x == 0)`
6565

6666
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
67-
--> $DIR/methods.rs:214:13
67+
--> $DIR/methods.rs:219:13
6868
|
6969
LL | let _ = v.iter().find(|&x| {
7070
| _____________^
@@ -74,13 +74,13 @@ LL | | ).is_some();
7474
| |______________________________^
7575

7676
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
77-
--> $DIR/methods.rs:220:22
77+
--> $DIR/methods.rs:225:22
7878
|
7979
LL | let _ = v.iter().position(|&x| x < 0).is_some();
8080
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `any(|&x| x < 0)`
8181

8282
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
83-
--> $DIR/methods.rs:223:13
83+
--> $DIR/methods.rs:228:13
8484
|
8585
LL | let _ = v.iter().position(|&x| {
8686
| _____________^
@@ -90,13 +90,13 @@ LL | | ).is_some();
9090
| |______________________________^
9191

9292
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
93-
--> $DIR/methods.rs:229:22
93+
--> $DIR/methods.rs:234:22
9494
|
9595
LL | let _ = v.iter().rposition(|&x| x < 0).is_some();
9696
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `any(|&x| x < 0)`
9797

9898
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
99-
--> $DIR/methods.rs:232:13
99+
--> $DIR/methods.rs:237:13
100100
|
101101
LL | let _ = v.iter().rposition(|&x| {
102102
| _____________^

0 commit comments

Comments
 (0)