better suggestions

This commit is contained in:
Takayuki Maeda 2021-09-27 00:30:39 +09:00
parent e2aad3fe60
commit 620f480e64
10 changed files with 70 additions and 53 deletions

View File

@ -781,19 +781,19 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
if imm_result && mut_result { if imm_result && mut_result {
err.span_suggestions( err.span_suggestions(
span, span.shrink_to_lo(),
"consider borrowing here", "consider borrowing here",
[format!("&{}", snippet), format!("&mut {}", snippet)].into_iter(), ["&".to_string(), "&mut ".to_string()].into_iter(),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
} else { } else {
err.span_suggestion( err.span_suggestion_verbose(
span, span.shrink_to_lo(),
&format!( &format!(
"consider{} borrowing here", "consider{} borrowing here",
if mut_result { " mutably" } else { "" } if mut_result { " mutably" } else { "" }
), ),
format!("&{}{}", if mut_result { "mut " } else { "" }, snippet), format!("&{}", if mut_result { "mut " } else { "" }),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
} }

View File

@ -2,10 +2,8 @@ error[E0277]: the trait bound `C: Copy` is not satisfied
--> $DIR/deriving-copyclone.rs:31:13 --> $DIR/deriving-copyclone.rs:31:13
| |
LL | is_copy(B { a: 1, b: C }); LL | is_copy(B { a: 1, b: C });
| ------- ^^^^^^^^^^^^^^^^ | ------- ^^^^^^^^^^^^^^^^ expected an implementor of trait `Copy`
| | | | |
| | expected an implementor of trait `Copy`
| | help: consider borrowing here: `&B { a: 1, b: C }`
| required by a bound introduced by this call | required by a bound introduced by this call
| |
note: required because of the requirements on the impl of `Copy` for `B<C>` note: required because of the requirements on the impl of `Copy` for `B<C>`
@ -19,15 +17,17 @@ note: required by a bound in `is_copy`
LL | fn is_copy<T: Copy>(_: T) {} LL | fn is_copy<T: Copy>(_: T) {}
| ^^^^ required by this bound in `is_copy` | ^^^^ required by this bound in `is_copy`
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider borrowing here
|
LL | is_copy(&B { a: 1, b: C });
| +
error[E0277]: the trait bound `C: Clone` is not satisfied error[E0277]: the trait bound `C: Clone` is not satisfied
--> $DIR/deriving-copyclone.rs:32:14 --> $DIR/deriving-copyclone.rs:32:14
| |
LL | is_clone(B { a: 1, b: C }); LL | is_clone(B { a: 1, b: C });
| -------- ^^^^^^^^^^^^^^^^ | -------- ^^^^^^^^^^^^^^^^ expected an implementor of trait `Clone`
| | | | |
| | expected an implementor of trait `Clone`
| | help: consider borrowing here: `&B { a: 1, b: C }`
| required by a bound introduced by this call | required by a bound introduced by this call
| |
note: required because of the requirements on the impl of `Clone` for `B<C>` note: required because of the requirements on the impl of `Clone` for `B<C>`
@ -41,15 +41,17 @@ note: required by a bound in `is_clone`
LL | fn is_clone<T: Clone>(_: T) {} LL | fn is_clone<T: Clone>(_: T) {}
| ^^^^^ required by this bound in `is_clone` | ^^^^^ required by this bound in `is_clone`
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider borrowing here
|
LL | is_clone(&B { a: 1, b: C });
| +
error[E0277]: the trait bound `D: Copy` is not satisfied error[E0277]: the trait bound `D: Copy` is not satisfied
--> $DIR/deriving-copyclone.rs:35:13 --> $DIR/deriving-copyclone.rs:35:13
| |
LL | is_copy(B { a: 1, b: D }); LL | is_copy(B { a: 1, b: D });
| ------- ^^^^^^^^^^^^^^^^ | ------- ^^^^^^^^^^^^^^^^ expected an implementor of trait `Copy`
| | | | |
| | expected an implementor of trait `Copy`
| | help: consider borrowing here: `&B { a: 1, b: D }`
| required by a bound introduced by this call | required by a bound introduced by this call
| |
note: required because of the requirements on the impl of `Copy` for `B<D>` note: required because of the requirements on the impl of `Copy` for `B<D>`
@ -63,6 +65,10 @@ note: required by a bound in `is_copy`
LL | fn is_copy<T: Copy>(_: T) {} LL | fn is_copy<T: Copy>(_: T) {}
| ^^^^ required by this bound in `is_copy` | ^^^^ required by this bound in `is_copy`
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider borrowing here
|
LL | is_copy(&B { a: 1, b: D });
| +
error: aborting due to 3 previous errors error: aborting due to 3 previous errors

View File

@ -2,10 +2,7 @@ error[E0277]: the size for values of type `dyn Iterator<Item = &'a mut u8>` cann
--> $DIR/issue-20605.rs:2:17 --> $DIR/issue-20605.rs:2:17
| |
LL | for item in *things { *item = 0 } LL | for item in *things { *item = 0 }
| ^^^^^^^ | ^^^^^^^ expected an implementor of trait `IntoIterator`
| |
| expected an implementor of trait `IntoIterator`
| help: consider mutably borrowing here: `&mut *things`
| |
= note: the trait bound `dyn Iterator<Item = &'a mut u8>: IntoIterator` is not satisfied = note: the trait bound `dyn Iterator<Item = &'a mut u8>: IntoIterator` is not satisfied
= note: required because of the requirements on the impl of `IntoIterator` for `dyn Iterator<Item = &'a mut u8>` = note: required because of the requirements on the impl of `IntoIterator` for `dyn Iterator<Item = &'a mut u8>`
@ -14,6 +11,10 @@ note: required by `into_iter`
| |
LL | fn into_iter(self) -> Self::IntoIter; LL | fn into_iter(self) -> Self::IntoIter;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider mutably borrowing here
|
LL | for item in &mut *things { *item = 0 }
| ++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -22,10 +22,8 @@ error[E0277]: the trait bound `S: Trait` is not satisfied
--> $DIR/imm-ref-trait-object-literal.rs:13:7 --> $DIR/imm-ref-trait-object-literal.rs:13:7
| |
LL | foo(s); LL | foo(s);
| --- ^ | --- ^ expected an implementor of trait `Trait`
| | | | |
| | expected an implementor of trait `Trait`
| | help: consider mutably borrowing here: `&mut s`
| required by a bound introduced by this call | required by a bound introduced by this call
| |
note: required by a bound in `foo` note: required by a bound in `foo`
@ -33,6 +31,10 @@ note: required by a bound in `foo`
| |
LL | fn foo<X: Trait>(_: X) {} LL | fn foo<X: Trait>(_: X) {}
| ^^^^^ required by this bound in `foo` | ^^^^^ required by this bound in `foo`
help: consider mutably borrowing here
|
LL | foo(&mut s);
| ++++
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View File

@ -2,10 +2,8 @@ error[E0277]: the trait bound `i32: Tr` is not satisfied
--> $DIR/issue-84973-2.rs:11:9 --> $DIR/issue-84973-2.rs:11:9
| |
LL | foo(a); LL | foo(a);
| --- ^ | --- ^ expected an implementor of trait `Tr`
| | | | |
| | expected an implementor of trait `Tr`
| | help: consider mutably borrowing here: `&mut a`
| required by a bound introduced by this call | required by a bound introduced by this call
| |
note: required by a bound in `foo` note: required by a bound in `foo`
@ -13,6 +11,10 @@ note: required by a bound in `foo`
| |
LL | fn foo<T: Tr>(i: T) {} LL | fn foo<T: Tr>(i: T) {}
| ^^ required by this bound in `foo` | ^^ required by this bound in `foo`
help: consider mutably borrowing here
|
LL | foo(&mut a);
| ++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -16,10 +16,8 @@ error[E0277]: the trait bound `f32: Tr` is not satisfied
--> $DIR/issue-84973-negative.rs:11:9 --> $DIR/issue-84973-negative.rs:11:9
| |
LL | bar(b); LL | bar(b);
| --- ^ | --- ^ expected an implementor of trait `Tr`
| | | | |
| | expected an implementor of trait `Tr`
| | help: consider borrowing here: `&b`
| required by a bound introduced by this call | required by a bound introduced by this call
| |
note: required by a bound in `bar` note: required by a bound in `bar`
@ -27,6 +25,10 @@ note: required by a bound in `bar`
| |
LL | fn bar<T: Tr>(t: T) {} LL | fn bar<T: Tr>(t: T) {}
| ^^ required by this bound in `bar` | ^^ required by this bound in `bar`
help: consider borrowing here
|
LL | bar(&b);
| +
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View File

@ -2,10 +2,8 @@ error[E0277]: the trait bound `Fancy: SomeTrait` is not satisfied
--> $DIR/issue-84973.rs:6:24 --> $DIR/issue-84973.rs:6:24
| |
LL | let o = Other::new(f); LL | let o = Other::new(f);
| ---------- ^ | ---------- ^ expected an implementor of trait `SomeTrait`
| | | | |
| | expected an implementor of trait `SomeTrait`
| | help: consider borrowing here: `&f`
| required by a bound introduced by this call | required by a bound introduced by this call
| |
note: required by `Other::<'a, G>::new` note: required by `Other::<'a, G>::new`
@ -13,6 +11,10 @@ note: required by `Other::<'a, G>::new`
| |
LL | pub fn new(g: G) -> Self { LL | pub fn new(g: G) -> Self {
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^
help: consider borrowing here
|
LL | let o = Other::new(&f);
| +
error: aborting due to previous error error: aborting due to previous error

View File

@ -14,9 +14,9 @@ LL | fn into_iter(self) -> Self::IntoIter;
help: consider borrowing here help: consider borrowing here
| |
LL | for _ in &v[1..] { LL | for _ in &v[1..] {
| ~~~~~~~ | +
LL | for _ in &mut v[1..] { LL | for _ in &mut v[1..] {
| ~~~~~~~~~~~ | ++++
error[E0277]: `[i32]` is not an iterator error[E0277]: `[i32]` is not an iterator
--> $DIR/slice-issue-87994.rs:3:12 --> $DIR/slice-issue-87994.rs:3:12
@ -34,9 +34,9 @@ LL | fn into_iter(self) -> Self::IntoIter;
help: consider borrowing here help: consider borrowing here
| |
LL | for _ in &v[1..] { LL | for _ in &v[1..] {
| ~~~~~~~ | +
LL | for _ in &mut v[1..] { LL | for _ in &mut v[1..] {
| ~~~~~~~~~~~ | ++++
error[E0277]: the size for values of type `[K]` cannot be known at compilation time error[E0277]: the size for values of type `[K]` cannot be known at compilation time
--> $DIR/slice-issue-87994.rs:11:13 --> $DIR/slice-issue-87994.rs:11:13
@ -54,9 +54,9 @@ LL | fn into_iter(self) -> Self::IntoIter;
help: consider borrowing here help: consider borrowing here
| |
LL | for i2 in &v2[1..] { LL | for i2 in &v2[1..] {
| ~~~~~~~~ | +
LL | for i2 in &mut v2[1..] { LL | for i2 in &mut v2[1..] {
| ~~~~~~~~~~~~ | ++++
error[E0277]: `[K]` is not an iterator error[E0277]: `[K]` is not an iterator
--> $DIR/slice-issue-87994.rs:11:13 --> $DIR/slice-issue-87994.rs:11:13
@ -74,9 +74,9 @@ LL | fn into_iter(self) -> Self::IntoIter;
help: consider borrowing here help: consider borrowing here
| |
LL | for i2 in &v2[1..] { LL | for i2 in &v2[1..] {
| ~~~~~~~~ | +
LL | for i2 in &mut v2[1..] { LL | for i2 in &mut v2[1..] {
| ~~~~~~~~~~~~ | ++++
error: aborting due to 4 previous errors error: aborting due to 4 previous errors

View File

@ -14,9 +14,9 @@ LL | fn foo<X: Trait>(_: X) {}
help: consider borrowing here help: consider borrowing here
| |
LL | foo(&s); LL | foo(&s);
| ~~ | +
LL | foo(&mut s); LL | foo(&mut s);
| ~~~~~~ | ++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -61,10 +61,8 @@ error[E0277]: `dummy2::TestType` cannot be sent between threads safely
--> $DIR/negated-auto-traits-error.rs:48:13 --> $DIR/negated-auto-traits-error.rs:48:13
| |
LL | is_send(Box::new(TestType)); LL | is_send(Box::new(TestType));
| ------- ^^^^^^^^^^^^^^^^^^ | ------- ^^^^^^^^^^^^^^^^^^ expected an implementor of trait `Send`
| | | | |
| | expected an implementor of trait `Send`
| | help: consider borrowing here: `&Box::new(TestType)`
| required by a bound introduced by this call | required by a bound introduced by this call
| |
= note: the trait bound `dummy2::TestType: Send` is not satisfied = note: the trait bound `dummy2::TestType: Send` is not satisfied
@ -75,6 +73,10 @@ note: required by a bound in `is_send`
| |
LL | fn is_send<T: Send>(_: T) {} LL | fn is_send<T: Send>(_: T) {}
| ^^^^ required by this bound in `is_send` | ^^^^ required by this bound in `is_send`
help: consider borrowing here
|
LL | is_send(&Box::new(TestType));
| +
error[E0277]: `dummy3::TestType` cannot be sent between threads safely error[E0277]: `dummy3::TestType` cannot be sent between threads safely
--> $DIR/negated-auto-traits-error.rs:56:13 --> $DIR/negated-auto-traits-error.rs:56:13
@ -120,9 +122,9 @@ LL | fn is_sync<T: Sync>(_: T) {}
help: consider borrowing here help: consider borrowing here
| |
LL | is_sync(&Outer2(TestType)); LL | is_sync(&Outer2(TestType));
| ~~~~~~~~~~~~~~~~~ | +
LL | is_sync(&mut Outer2(TestType)); LL | is_sync(&mut Outer2(TestType));
| ~~~~~~~~~~~~~~~~~~~~~ | ++++
error: aborting due to 7 previous errors error: aborting due to 7 previous errors