Correct suggestion when dereferencing enough, calling a function

This commit is contained in:
ThibsG 2021-09-19 08:08:54 +02:00
parent ac45a83ad5
commit 6d1ccbf466
7 changed files with 47 additions and 8 deletions

View File

@ -248,7 +248,6 @@ fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, _: ty::BorrowKind) {
let expr = self.cx.tcx.hir().expect_expr(cmt.hir_id); let expr = self.cx.tcx.hir().expect_expr(cmt.hir_id);
let arg_ty_kind = self.cx.typeck_results().expr_ty(expr).kind(); 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)) { if matches!(arg_ty_kind, ty::Ref(_, _, Mutability::Not)) {
let start_span = Span::new(self.next_pos, span.lo(), span.ctxt()); let start_span = Span::new(self.next_pos, span.lo(), span.ctxt());
let start_snip = let start_snip =
@ -261,10 +260,10 @@ fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, _: HirId, _: ty::BorrowKind) {
}; };
self.suggestion_start.push_str(&ident_sugg); self.suggestion_start.push_str(&ident_sugg);
self.next_pos = span.hi(); self.next_pos = span.hi();
return;
} else { } else {
self.applicability = Applicability::Unspecified; self.applicability = Applicability::Unspecified;
} }
return;
} }
} }

View File

@ -102,10 +102,17 @@ mod issue7392 {
*x == 9 *x == 9
} }
fn simple_fn(x: u32) -> bool {
x == 78
}
fn more_projections() { fn more_projections() {
let x = 19; let x = 19;
let ppx: &u32 = &x; let ppx: &u32 = &x;
let _ = ![ppx].iter().any(|ppp_x: &&u32| please(ppp_x)); let _ = ![ppx].iter().any(|ppp_x: &&u32| please(ppp_x));
let _ = ![String::from("Hey hey")].iter().any(|s| s.len() == 2); 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));
} }
} }

View File

@ -106,10 +106,17 @@ fn please(x: &u32) -> bool {
*x == 9 *x == 9
} }
fn simple_fn(x: u32) -> bool {
x == 78
}
fn more_projections() { fn more_projections() {
let x = 19; let x = 19;
let ppx: &u32 = &x; let ppx: &u32 = &x;
let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_none(); 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 _ = [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();
} }
} }

View File

@ -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)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!vfoo.iter().any(|sub| sub[1..4].len() == 3)`
error: called `is_none()` after searching an `Iterator` with `find` 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(); 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))` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `![ppx].iter().any(|ppp_x: &&u32| please(ppp_x))`
error: called `is_none()` after searching an `Iterator` with `find` 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(); 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)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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

View File

@ -103,10 +103,17 @@ mod issue7392 {
*x == 9 *x == 9
} }
fn simple_fn(x: u32) -> bool {
x == 78
}
fn more_projections() { fn more_projections() {
let x = 19; let x = 19;
let ppx: &u32 = &x; let ppx: &u32 = &x;
let _ = [ppx].iter().any(|ppp_x: &&u32| please(ppp_x)); let _ = [ppx].iter().any(|ppp_x: &&u32| please(ppp_x));
let _ = [String::from("Hey hey")].iter().any(|s| s.len() == 2); 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));
} }
} }

View File

@ -105,10 +105,17 @@ fn please(x: &u32) -> bool {
*x == 9 *x == 9
} }
fn simple_fn(x: u32) -> bool {
x == 78
}
fn more_projections() { fn more_projections() {
let x = 19; let x = 19;
let ppx: &u32 = &x; let ppx: &u32 = &x;
let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_some(); 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 _ = [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();
} }
} }

View File

@ -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)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|sub| sub[1..4].len() == 3)`
error: called `is_some()` after searching an `Iterator` with `find` 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(); LL | let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|ppp_x: &&u32| please(ppp_x))` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|ppp_x: &&u32| please(ppp_x))`
error: called `is_some()` after searching an `Iterator` with `find` 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(); LL | let _ = [String::from("Hey hey")].iter().find(|s| s.len() == 2).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `any()` instead: `any(|s| s.len() == 2)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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