Skip to content

Commit

Permalink
Correct suggestion when dereferencing enough, calling a function
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibsG committed Nov 20, 2021
1 parent ac45a83 commit 6d1ccbf
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 8 deletions.
3 changes: 1 addition & 2 deletions clippy_lints/src/methods/search_is_some.rs
Expand Up @@ -248,7 +248,6 @@ impl<'tcx> Delegate<'tcx> for DerefDelegate<'_, 'tcx> {
let expr = self.cx.tcx.hir().expect_expr(cmt.hir_id);
let arg_ty_kind = self.cx.typeck_results().expr_ty(expr).kind();

// Note: this should always be true, as `find` only gives us a reference which are not mutable
if matches!(arg_ty_kind, ty::Ref(_, _, Mutability::Not)) {
let start_span = Span::new(self.next_pos, span.lo(), span.ctxt());
let start_snip =
Expand All @@ -261,10 +260,10 @@ impl<'tcx> Delegate<'tcx> for DerefDelegate<'_, 'tcx> {
};
self.suggestion_start.push_str(&ident_sugg);
self.next_pos = span.hi();
return;
} else {
self.applicability = Applicability::Unspecified;
}
return;
}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/ui/search_is_some_fixable_none.fixed
Expand Up @@ -102,10 +102,17 @@ mod issue7392 {
*x == 9
}

fn simple_fn(x: u32) -> bool {
x == 78
}

fn more_projections() {
let x = 19;
let ppx: &u32 = &x;
let _ = ![ppx].iter().any(|ppp_x: &&u32| please(ppp_x));
let _ = ![String::from("Hey hey")].iter().any(|s| s.len() == 2);

let v = vec![3, 2, 1, 0];
let _ = !v.iter().any(|x| simple_fn(*x));
}
}
7 changes: 7 additions & 0 deletions tests/ui/search_is_some_fixable_none.rs
Expand Up @@ -106,10 +106,17 @@ mod issue7392 {
*x == 9
}

fn simple_fn(x: u32) -> bool {
x == 78
}

fn more_projections() {
let x = 19;
let ppx: &u32 = &x;
let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_none();
let _ = [String::from("Hey hey")].iter().find(|s| s.len() == 2).is_none();

let v = vec![3, 2, 1, 0];
let _ = v.iter().find(|x| simple_fn(**x)).is_none();
}
}
12 changes: 9 additions & 3 deletions tests/ui/search_is_some_fixable_none.stderr
Expand Up @@ -170,16 +170,22 @@ LL | let _ = vfoo.iter().find(|sub| sub[1..4].len() == 3).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!vfoo.iter().any(|sub| sub[1..4].len() == 3)`

error: called `is_none()` after searching an `Iterator` with `find`
--> $DIR/search_is_some_fixable_none.rs:112:17
--> $DIR/search_is_some_fixable_none.rs:116:17
|
LL | let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `![ppx].iter().any(|ppp_x: &&u32| please(ppp_x))`

error: called `is_none()` after searching an `Iterator` with `find`
--> $DIR/search_is_some_fixable_none.rs:113:17
--> $DIR/search_is_some_fixable_none.rs:117:17
|
LL | let _ = [String::from("Hey hey")].iter().find(|s| s.len() == 2).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `![String::from("Hey hey")].iter().any(|s| s.len() == 2)`

error: aborting due to 28 previous errors
error: called `is_none()` after searching an `Iterator` with `find`
--> $DIR/search_is_some_fixable_none.rs:120:17
|
LL | let _ = v.iter().find(|x| simple_fn(**x)).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|x| simple_fn(*x))`

error: aborting due to 29 previous errors

7 changes: 7 additions & 0 deletions tests/ui/search_is_some_fixable_some.fixed
Expand Up @@ -103,10 +103,17 @@ mod issue7392 {
*x == 9
}

fn simple_fn(x: u32) -> bool {
x == 78
}

fn more_projections() {
let x = 19;
let ppx: &u32 = &x;
let _ = [ppx].iter().any(|ppp_x: &&u32| please(ppp_x));
let _ = [String::from("Hey hey")].iter().any(|s| s.len() == 2);

let v = vec![3, 2, 1, 0];
let _ = v.iter().any(|x| simple_fn(*x));
}
}
7 changes: 7 additions & 0 deletions tests/ui/search_is_some_fixable_some.rs
Expand Up @@ -105,10 +105,17 @@ mod issue7392 {
*x == 9
}

fn simple_fn(x: u32) -> bool {
x == 78
}

fn more_projections() {
let x = 19;
let ppx: &u32 = &x;
let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_some();
let _ = [String::from("Hey hey")].iter().find(|s| s.len() == 2).is_some();

let v = vec![3, 2, 1, 0];
let _ = v.iter().find(|x| simple_fn(**x)).is_some();
}
}
12 changes: 9 additions & 3 deletions tests/ui/search_is_some_fixable_some.stderr
Expand Up @@ -161,16 +161,22 @@ LL | let _ = vfoo.iter().find(|sub| sub[1..4].len() == 3).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|sub| sub[1..4].len() == 3)`

error: called `is_some()` after searching an `Iterator` with `find`
--> $DIR/search_is_some_fixable_some.rs:111:30
--> $DIR/search_is_some_fixable_some.rs:115:30
|
LL | let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|ppp_x: &&u32| please(ppp_x))`

error: called `is_some()` after searching an `Iterator` with `find`
--> $DIR/search_is_some_fixable_some.rs:112:50
--> $DIR/search_is_some_fixable_some.rs:116:50
|
LL | let _ = [String::from("Hey hey")].iter().find(|s| s.len() == 2).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|s| s.len() == 2)`

error: aborting due to 28 previous errors
error: called `is_some()` after searching an `Iterator` with `find`
--> $DIR/search_is_some_fixable_some.rs:119:26
|
LL | let _ = v.iter().find(|x| simple_fn(**x)).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|x| simple_fn(*x))`

error: aborting due to 29 previous errors

0 comments on commit 6d1ccbf

Please sign in to comment.