Tweak borrow suggestion

This commit is contained in:
Michael Goulet 2023-04-18 19:44:27 +00:00
parent ad6b20bf52
commit a9051d861c
26 changed files with 288 additions and 211 deletions

View File

@ -1334,52 +1334,55 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
)); ));
} }
if let Ok(src) = sm.span_to_snippet(sugg_sp) { let needs_parens = match expr.kind {
let needs_parens = match expr.kind { // parenthesize if needed (Issue #46756)
// parenthesize if needed (Issue #46756) hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true,
hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true, // parenthesize borrows of range literals (Issue #54505)
// parenthesize borrows of range literals (Issue #54505) _ if is_range_literal(expr) => true,
_ if is_range_literal(expr) => true, _ => false,
_ => false, };
};
if let Some(sugg) = self.can_use_as_ref(expr) { if let Some(sugg) = self.can_use_as_ref(expr) {
return Some((
sugg.0,
sugg.1.to_string(),
sugg.2,
Applicability::MachineApplicable,
false,
false,
));
}
let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) {
Some(ident) => format!("{ident}: "),
None => String::new(),
};
if let Some(hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Assign(..),
..
})) = self.tcx.hir().find_parent(expr.hir_id)
{
if mutability.is_mut() {
// Suppressing this diagnostic, we'll properly print it in `check_expr_assign`
return None;
}
}
let sugg_expr = if needs_parens { format!("({src})") } else { src };
return Some(( return Some((
sp, sugg.0,
format!("consider {}borrowing here", mutability.mutably_str()), sugg.1.to_string(),
format!("{prefix}{}{sugg_expr}", mutability.ref_prefix_str()), sugg.2,
Applicability::MachineApplicable, Applicability::MachineApplicable,
false, false,
false, false,
)); ));
} }
let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) {
Some(ident) => format!("{ident}: "),
None => String::new(),
};
if let Some(hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Assign(..),
..
})) = self.tcx.hir().find_parent(expr.hir_id)
{
if mutability.is_mut() {
// Suppressing this diagnostic, we'll properly print it in `check_expr_assign`
return None;
}
}
let (sp, sugg_expr, verbose) = if needs_parens {
let src = sm.span_to_snippet(sugg_sp).ok()?;
(sp, format!("({src})"), false)
} else {
(sp.shrink_to_lo(), "".to_string(), true)
};
return Some((
sp,
format!("consider {}borrowing here", mutability.mutably_str()),
format!("{prefix}{}{sugg_expr}", mutability.ref_prefix_str()),
Applicability::MachineApplicable,
verbose,
false,
));
} }
} }
( (

View File

@ -16,7 +16,7 @@ LL | fn foo(a: &A, d: D, e: &E, g: G) {}
help: consider borrowing here help: consider borrowing here
| |
LL | foo(&&A, B, C, D, &E, F, G); LL | foo(&&A, B, C, D, &E, F, G);
| ~~ | +
help: remove the extra arguments help: remove the extra arguments
| |
LL - foo(&&A, B, C, D, E, F, G); LL - foo(&&A, B, C, D, E, F, G);

View File

@ -2,14 +2,16 @@ error[E0308]: mismatched types
--> $DIR/issue-102206.rs:6:27 --> $DIR/issue-102206.rs:6:27
| |
LL | std::mem::size_of_val(foo()); LL | std::mem::size_of_val(foo());
| --------------------- ^^^^^ | --------------------- ^^^^^ expected `&_`, found future
| | | | |
| | expected `&_`, found future
| | help: consider borrowing here: `&foo()`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
note: function defined here note: function defined here
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL --> $SRC_DIR/core/src/mem/mod.rs:LL:COL
help: consider borrowing here
|
LL | std::mem::size_of_val(&foo());
| +
error: aborting due to previous error error: aborting due to previous error

View File

@ -2,11 +2,14 @@ error[E0308]: mismatched types
--> $DIR/coercion-slice.rs:4:21 --> $DIR/coercion-slice.rs:4:21
| |
LL | let _: &[i32] = [0]; LL | let _: &[i32] = [0];
| ------ ^^^ | ------ ^^^ expected `&[i32]`, found `[{integer}; 1]`
| | | | |
| | expected `&[i32]`, found `[{integer}; 1]`
| | help: consider borrowing here: `&[0]`
| expected due to this | expected due to this
|
help: consider borrowing here
|
LL | let _: &[i32] = &[0];
| +
error: aborting due to previous error error: aborting due to previous error

View File

@ -98,19 +98,23 @@ error[E0308]: mismatched types
--> $DIR/deref-suggestion.rs:40:17 --> $DIR/deref-suggestion.rs:40:17
| |
LL | let s = S { u }; LL | let s = S { u };
| ^ | ^ expected `&u32`, found integer
| | |
| expected `&u32`, found integer help: consider borrowing here
| help: consider borrowing here: `u: &u` |
LL | let s = S { u: &u };
| ++++
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/deref-suggestion.rs:42:20 --> $DIR/deref-suggestion.rs:42:20
| |
LL | let s = S { u: u }; LL | let s = S { u: u };
| ^ | ^ expected `&u32`, found integer
| | |
| expected `&u32`, found integer help: consider borrowing here
| help: consider borrowing here: `&u` |
LL | let s = S { u: &u };
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/deref-suggestion.rs:45:17 --> $DIR/deref-suggestion.rs:45:17

View File

@ -2,10 +2,8 @@ error[E0308]: mismatched types
--> $DIR/issue-11374.rs:26:15 --> $DIR/issue-11374.rs:26:15
| |
LL | c.read_to(v); LL | c.read_to(v);
| ------- ^ | ------- ^ expected `&mut [u8]`, found `Vec<_>`
| | | | |
| | expected `&mut [u8]`, found `Vec<_>`
| | help: consider mutably borrowing here: `&mut v`
| arguments to this method are incorrect | arguments to this method are incorrect
| |
= note: expected mutable reference `&mut [u8]` = note: expected mutable reference `&mut [u8]`
@ -15,6 +13,10 @@ note: method defined here
| |
LL | pub fn read_to(&mut self, vec: &mut [u8]) { LL | pub fn read_to(&mut self, vec: &mut [u8]) {
| ^^^^^^^ -------------- | ^^^^^^^ --------------
help: consider mutably borrowing here
|
LL | c.read_to(&mut v);
| ++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -2,11 +2,14 @@ error[E0308]: mismatched types
--> $DIR/issue-17033.rs:2:10 --> $DIR/issue-17033.rs:2:10
| |
LL | (*p)(()) LL | (*p)(())
| ---- ^^ | ---- ^^ expected `&mut ()`, found `()`
| | | | |
| | expected `&mut ()`, found `()`
| | help: consider mutably borrowing here: `&mut ()`
| arguments to this function are incorrect | arguments to this function are incorrect
|
help: consider mutably borrowing here
|
LL | (*p)(&mut ())
| ++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -19,7 +19,7 @@ LL | fn print_x(_: &dyn Foo<Item=bool>, extra: &str) {
help: consider borrowing here help: consider borrowing here
| |
LL | print_x(&X); LL | print_x(&X);
| ~~ | +
help: provide the argument help: provide the argument
| |
LL | print_x(/* &dyn Foo<Item = bool> */, /* &str */); LL | print_x(/* &dyn Foo<Item = bool> */, /* &str */);

View File

@ -2,10 +2,12 @@ error[E0308]: mismatched types
--> $DIR/issue-46302.rs:3:27 --> $DIR/issue-46302.rs:3:27
| |
LL | let u: &str = if true { s[..2] } else { s }; LL | let u: &str = if true { s[..2] } else { s };
| ^^^^^^ | ^^^^^^ expected `&str`, found `str`
| | |
| expected `&str`, found `str` help: consider borrowing here
| help: consider borrowing here: `&s[..2]` |
LL | let u: &str = if true { &s[..2] } else { s };
| +
error: aborting due to previous error error: aborting due to previous error

View File

@ -2,10 +2,8 @@ error[E0308]: mismatched types
--> $DIR/issue-61106.rs:3:9 --> $DIR/issue-61106.rs:3:9
| |
LL | foo(x.clone()); LL | foo(x.clone());
| --- ^^^^^^^^^ | --- ^^^^^^^^^ expected `&str`, found `String`
| | | | |
| | expected `&str`, found `String`
| | help: consider borrowing here: `&x`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
note: function defined here note: function defined here
@ -13,6 +11,10 @@ note: function defined here
| |
LL | fn foo(_: &str) {} LL | fn foo(_: &str) {}
| ^^^ ------- | ^^^ -------
help: consider borrowing here
|
LL | foo(&x.clone());
| +
error: aborting due to previous error error: aborting due to previous error

View File

@ -2,10 +2,8 @@ error[E0308]: mismatched types
--> $DIR/method-self-arg-1.rs:11:14 --> $DIR/method-self-arg-1.rs:11:14
| |
LL | Foo::bar(x); LL | Foo::bar(x);
| -------- ^ | -------- ^ expected `&Foo`, found `Foo`
| | | | |
| | expected `&Foo`, found `Foo`
| | help: consider borrowing here: `&x`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
note: method defined here note: method defined here
@ -13,6 +11,10 @@ note: method defined here
| |
LL | fn bar(&self) {} LL | fn bar(&self) {}
| ^^^ ----- | ^^^ -----
help: consider borrowing here
|
LL | Foo::bar(&x);
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/method-self-arg-1.rs:13:14 --> $DIR/method-self-arg-1.rs:13:14

View File

@ -2,10 +2,8 @@ error[E0308]: mismatched types
--> $DIR/dont-point-return-on-E0308.rs:11:11 --> $DIR/dont-point-return-on-E0308.rs:11:11
| |
LL | f(()); LL | f(());
| - ^^ | - ^^ expected `&()`, found `()`
| | | | |
| | expected `&()`, found `()`
| | help: consider borrowing here: `&()`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
note: function defined here note: function defined here
@ -13,6 +11,10 @@ note: function defined here
| |
LL | async fn f(_: &()) {} LL | async fn f(_: &()) {}
| ^ ------ | ^ ------
help: consider borrowing here
|
LL | f(&());
| +
error: aborting due to previous error error: aborting due to previous error

View File

@ -2,10 +2,8 @@ error[E0308]: mismatched types
--> $DIR/mut-cross-borrowing.rs:7:7 --> $DIR/mut-cross-borrowing.rs:7:7
| |
LL | f(x) LL | f(x)
| - ^ | - ^ expected `&mut isize`, found `Box<{integer}>`
| | | | |
| | expected `&mut isize`, found `Box<{integer}>`
| | help: consider mutably borrowing here: `&mut x`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected mutable reference `&mut isize` = note: expected mutable reference `&mut isize`
@ -15,6 +13,10 @@ note: function defined here
| |
LL | fn f(_: &mut isize) {} LL | fn f(_: &mut isize) {}
| ^ ------------- | ^ -------------
help: consider mutably borrowing here
|
LL | f(&mut x)
| ++++
error: aborting due to previous error error: aborting due to previous error

View File

@ -16,60 +16,60 @@ fn main() {
take_range(&std::ops::Range { start: 0, end: 1 }); take_range(&std::ops::Range { start: 0, end: 1 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &std::ops::Range { start: 0, end: 1 } //~| SUGGESTION &
take_range(&::std::ops::Range { start: 0, end: 1 }); take_range(&::std::ops::Range { start: 0, end: 1 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &::std::ops::Range { start: 0, end: 1 } //~| SUGGESTION &
take_range(&std::ops::RangeFrom { start: 1 }); take_range(&std::ops::RangeFrom { start: 1 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &std::ops::RangeFrom { start: 1 } //~| SUGGESTION &
take_range(&::std::ops::RangeFrom { start: 1 }); take_range(&::std::ops::RangeFrom { start: 1 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &::std::ops::RangeFrom { start: 1 } //~| SUGGESTION &
take_range(&std::ops::RangeFull {}); take_range(&std::ops::RangeFull {});
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &std::ops::RangeFull {} //~| SUGGESTION &
take_range(&::std::ops::RangeFull {}); take_range(&::std::ops::RangeFull {});
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &::std::ops::RangeFull {} //~| SUGGESTION &
take_range(&std::ops::RangeInclusive::new(0, 1)); take_range(&std::ops::RangeInclusive::new(0, 1));
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &std::ops::RangeInclusive::new(0, 1) //~| SUGGESTION &
take_range(&::std::ops::RangeInclusive::new(0, 1)); take_range(&::std::ops::RangeInclusive::new(0, 1));
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &::std::ops::RangeInclusive::new(0, 1) //~| SUGGESTION &
take_range(&std::ops::RangeTo { end: 5 }); take_range(&std::ops::RangeTo { end: 5 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &std::ops::RangeTo { end: 5 } //~| SUGGESTION &
take_range(&::std::ops::RangeTo { end: 5 }); take_range(&::std::ops::RangeTo { end: 5 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &::std::ops::RangeTo { end: 5 } //~| SUGGESTION &
take_range(&std::ops::RangeToInclusive { end: 5 }); take_range(&std::ops::RangeToInclusive { end: 5 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &std::ops::RangeToInclusive { end: 5 } //~| SUGGESTION &
take_range(&::std::ops::RangeToInclusive { end: 5 }); take_range(&::std::ops::RangeToInclusive { end: 5 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &::std::ops::RangeToInclusive { end: 5 } //~| SUGGESTION &
} }

View File

@ -16,60 +16,60 @@ fn main() {
take_range(std::ops::Range { start: 0, end: 1 }); take_range(std::ops::Range { start: 0, end: 1 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &std::ops::Range { start: 0, end: 1 } //~| SUGGESTION &
take_range(::std::ops::Range { start: 0, end: 1 }); take_range(::std::ops::Range { start: 0, end: 1 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &::std::ops::Range { start: 0, end: 1 } //~| SUGGESTION &
take_range(std::ops::RangeFrom { start: 1 }); take_range(std::ops::RangeFrom { start: 1 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &std::ops::RangeFrom { start: 1 } //~| SUGGESTION &
take_range(::std::ops::RangeFrom { start: 1 }); take_range(::std::ops::RangeFrom { start: 1 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &::std::ops::RangeFrom { start: 1 } //~| SUGGESTION &
take_range(std::ops::RangeFull {}); take_range(std::ops::RangeFull {});
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &std::ops::RangeFull {} //~| SUGGESTION &
take_range(::std::ops::RangeFull {}); take_range(::std::ops::RangeFull {});
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &::std::ops::RangeFull {} //~| SUGGESTION &
take_range(std::ops::RangeInclusive::new(0, 1)); take_range(std::ops::RangeInclusive::new(0, 1));
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &std::ops::RangeInclusive::new(0, 1) //~| SUGGESTION &
take_range(::std::ops::RangeInclusive::new(0, 1)); take_range(::std::ops::RangeInclusive::new(0, 1));
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &::std::ops::RangeInclusive::new(0, 1) //~| SUGGESTION &
take_range(std::ops::RangeTo { end: 5 }); take_range(std::ops::RangeTo { end: 5 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &std::ops::RangeTo { end: 5 } //~| SUGGESTION &
take_range(::std::ops::RangeTo { end: 5 }); take_range(::std::ops::RangeTo { end: 5 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &::std::ops::RangeTo { end: 5 } //~| SUGGESTION &
take_range(std::ops::RangeToInclusive { end: 5 }); take_range(std::ops::RangeToInclusive { end: 5 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &std::ops::RangeToInclusive { end: 5 } //~| SUGGESTION &
take_range(::std::ops::RangeToInclusive { end: 5 }); take_range(::std::ops::RangeToInclusive { end: 5 });
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider borrowing here //~| HELP consider borrowing here
//~| SUGGESTION &::std::ops::RangeToInclusive { end: 5 } //~| SUGGESTION &
} }

View File

@ -2,10 +2,8 @@ error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:16:16 --> $DIR/issue-54505-no-literals.rs:16:16
| |
LL | take_range(std::ops::Range { start: 0, end: 1 }); LL | take_range(std::ops::Range { start: 0, end: 1 });
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `Range<{integer}>`
| | | | |
| | expected `&_`, found `Range<{integer}>`
| | help: consider borrowing here: `&std::ops::Range { start: 0, end: 1 }`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected reference `&_` = note: expected reference `&_`
@ -15,15 +13,17 @@ note: function defined here
| |
LL | fn take_range(_r: &impl RangeBounds<i8>) {} LL | fn take_range(_r: &impl RangeBounds<i8>) {}
| ^^^^^^^^^^ ------------------------- | ^^^^^^^^^^ -------------------------
help: consider borrowing here
|
LL | take_range(&std::ops::Range { start: 0, end: 1 });
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:21:16 --> $DIR/issue-54505-no-literals.rs:21:16
| |
LL | take_range(::std::ops::Range { start: 0, end: 1 }); LL | take_range(::std::ops::Range { start: 0, end: 1 });
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `Range<{integer}>`
| | | | |
| | expected `&_`, found `Range<{integer}>`
| | help: consider borrowing here: `&::std::ops::Range { start: 0, end: 1 }`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected reference `&_` = note: expected reference `&_`
@ -33,15 +33,17 @@ note: function defined here
| |
LL | fn take_range(_r: &impl RangeBounds<i8>) {} LL | fn take_range(_r: &impl RangeBounds<i8>) {}
| ^^^^^^^^^^ ------------------------- | ^^^^^^^^^^ -------------------------
help: consider borrowing here
|
LL | take_range(&::std::ops::Range { start: 0, end: 1 });
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:26:16 --> $DIR/issue-54505-no-literals.rs:26:16
| |
LL | take_range(std::ops::RangeFrom { start: 1 }); LL | take_range(std::ops::RangeFrom { start: 1 });
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeFrom<{integer}>`
| | | | |
| | expected `&_`, found `RangeFrom<{integer}>`
| | help: consider borrowing here: `&std::ops::RangeFrom { start: 1 }`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected reference `&_` = note: expected reference `&_`
@ -51,15 +53,17 @@ note: function defined here
| |
LL | fn take_range(_r: &impl RangeBounds<i8>) {} LL | fn take_range(_r: &impl RangeBounds<i8>) {}
| ^^^^^^^^^^ ------------------------- | ^^^^^^^^^^ -------------------------
help: consider borrowing here
|
LL | take_range(&std::ops::RangeFrom { start: 1 });
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:31:16 --> $DIR/issue-54505-no-literals.rs:31:16
| |
LL | take_range(::std::ops::RangeFrom { start: 1 }); LL | take_range(::std::ops::RangeFrom { start: 1 });
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeFrom<{integer}>`
| | | | |
| | expected `&_`, found `RangeFrom<{integer}>`
| | help: consider borrowing here: `&::std::ops::RangeFrom { start: 1 }`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected reference `&_` = note: expected reference `&_`
@ -69,15 +73,17 @@ note: function defined here
| |
LL | fn take_range(_r: &impl RangeBounds<i8>) {} LL | fn take_range(_r: &impl RangeBounds<i8>) {}
| ^^^^^^^^^^ ------------------------- | ^^^^^^^^^^ -------------------------
help: consider borrowing here
|
LL | take_range(&::std::ops::RangeFrom { start: 1 });
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:36:16 --> $DIR/issue-54505-no-literals.rs:36:16
| |
LL | take_range(std::ops::RangeFull {}); LL | take_range(std::ops::RangeFull {});
| ---------- ^^^^^^^^^^^^^^^^^^^^^^ | ---------- ^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeFull`
| | | | |
| | expected `&_`, found `RangeFull`
| | help: consider borrowing here: `&std::ops::RangeFull {}`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected reference `&_` = note: expected reference `&_`
@ -87,15 +93,17 @@ note: function defined here
| |
LL | fn take_range(_r: &impl RangeBounds<i8>) {} LL | fn take_range(_r: &impl RangeBounds<i8>) {}
| ^^^^^^^^^^ ------------------------- | ^^^^^^^^^^ -------------------------
help: consider borrowing here
|
LL | take_range(&std::ops::RangeFull {});
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:41:16 --> $DIR/issue-54505-no-literals.rs:41:16
| |
LL | take_range(::std::ops::RangeFull {}); LL | take_range(::std::ops::RangeFull {});
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^ | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeFull`
| | | | |
| | expected `&_`, found `RangeFull`
| | help: consider borrowing here: `&::std::ops::RangeFull {}`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected reference `&_` = note: expected reference `&_`
@ -105,15 +113,17 @@ note: function defined here
| |
LL | fn take_range(_r: &impl RangeBounds<i8>) {} LL | fn take_range(_r: &impl RangeBounds<i8>) {}
| ^^^^^^^^^^ ------------------------- | ^^^^^^^^^^ -------------------------
help: consider borrowing here
|
LL | take_range(&::std::ops::RangeFull {});
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:46:16 --> $DIR/issue-54505-no-literals.rs:46:16
| |
LL | take_range(std::ops::RangeInclusive::new(0, 1)); LL | take_range(std::ops::RangeInclusive::new(0, 1));
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeInclusive<{integer}>`
| | | | |
| | expected `&_`, found `RangeInclusive<{integer}>`
| | help: consider borrowing here: `&std::ops::RangeInclusive::new(0, 1)`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected reference `&_` = note: expected reference `&_`
@ -123,15 +133,17 @@ note: function defined here
| |
LL | fn take_range(_r: &impl RangeBounds<i8>) {} LL | fn take_range(_r: &impl RangeBounds<i8>) {}
| ^^^^^^^^^^ ------------------------- | ^^^^^^^^^^ -------------------------
help: consider borrowing here
|
LL | take_range(&std::ops::RangeInclusive::new(0, 1));
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:51:16 --> $DIR/issue-54505-no-literals.rs:51:16
| |
LL | take_range(::std::ops::RangeInclusive::new(0, 1)); LL | take_range(::std::ops::RangeInclusive::new(0, 1));
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeInclusive<{integer}>`
| | | | |
| | expected `&_`, found `RangeInclusive<{integer}>`
| | help: consider borrowing here: `&::std::ops::RangeInclusive::new(0, 1)`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected reference `&_` = note: expected reference `&_`
@ -141,15 +153,17 @@ note: function defined here
| |
LL | fn take_range(_r: &impl RangeBounds<i8>) {} LL | fn take_range(_r: &impl RangeBounds<i8>) {}
| ^^^^^^^^^^ ------------------------- | ^^^^^^^^^^ -------------------------
help: consider borrowing here
|
LL | take_range(&::std::ops::RangeInclusive::new(0, 1));
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:56:16 --> $DIR/issue-54505-no-literals.rs:56:16
| |
LL | take_range(std::ops::RangeTo { end: 5 }); LL | take_range(std::ops::RangeTo { end: 5 });
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeTo<{integer}>`
| | | | |
| | expected `&_`, found `RangeTo<{integer}>`
| | help: consider borrowing here: `&std::ops::RangeTo { end: 5 }`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected reference `&_` = note: expected reference `&_`
@ -159,15 +173,17 @@ note: function defined here
| |
LL | fn take_range(_r: &impl RangeBounds<i8>) {} LL | fn take_range(_r: &impl RangeBounds<i8>) {}
| ^^^^^^^^^^ ------------------------- | ^^^^^^^^^^ -------------------------
help: consider borrowing here
|
LL | take_range(&std::ops::RangeTo { end: 5 });
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:61:16 --> $DIR/issue-54505-no-literals.rs:61:16
| |
LL | take_range(::std::ops::RangeTo { end: 5 }); LL | take_range(::std::ops::RangeTo { end: 5 });
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeTo<{integer}>`
| | | | |
| | expected `&_`, found `RangeTo<{integer}>`
| | help: consider borrowing here: `&::std::ops::RangeTo { end: 5 }`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected reference `&_` = note: expected reference `&_`
@ -177,15 +193,17 @@ note: function defined here
| |
LL | fn take_range(_r: &impl RangeBounds<i8>) {} LL | fn take_range(_r: &impl RangeBounds<i8>) {}
| ^^^^^^^^^^ ------------------------- | ^^^^^^^^^^ -------------------------
help: consider borrowing here
|
LL | take_range(&::std::ops::RangeTo { end: 5 });
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:66:16 --> $DIR/issue-54505-no-literals.rs:66:16
| |
LL | take_range(std::ops::RangeToInclusive { end: 5 }); LL | take_range(std::ops::RangeToInclusive { end: 5 });
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeToInclusive<{integer}>`
| | | | |
| | expected `&_`, found `RangeToInclusive<{integer}>`
| | help: consider borrowing here: `&std::ops::RangeToInclusive { end: 5 }`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected reference `&_` = note: expected reference `&_`
@ -195,15 +213,17 @@ note: function defined here
| |
LL | fn take_range(_r: &impl RangeBounds<i8>) {} LL | fn take_range(_r: &impl RangeBounds<i8>) {}
| ^^^^^^^^^^ ------------------------- | ^^^^^^^^^^ -------------------------
help: consider borrowing here
|
LL | take_range(&std::ops::RangeToInclusive { end: 5 });
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-54505-no-literals.rs:71:16 --> $DIR/issue-54505-no-literals.rs:71:16
| |
LL | take_range(::std::ops::RangeToInclusive { end: 5 }); LL | take_range(::std::ops::RangeToInclusive { end: 5 });
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&_`, found `RangeToInclusive<{integer}>`
| | | | |
| | expected `&_`, found `RangeToInclusive<{integer}>`
| | help: consider borrowing here: `&::std::ops::RangeToInclusive { end: 5 }`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected reference `&_` = note: expected reference `&_`
@ -213,6 +233,10 @@ note: function defined here
| |
LL | fn take_range(_r: &impl RangeBounds<i8>) {} LL | fn take_range(_r: &impl RangeBounds<i8>) {}
| ^^^^^^^^^^ ------------------------- | ^^^^^^^^^^ -------------------------
help: consider borrowing here
|
LL | take_range(&::std::ops::RangeToInclusive { end: 5 });
| +
error: aborting due to 12 previous errors error: aborting due to 12 previous errors

View File

@ -10,11 +10,14 @@ error[E0308]: mismatched types
--> $DIR/coerce-suggestions.rs:9:19 --> $DIR/coerce-suggestions.rs:9:19
| |
LL | let x: &str = String::new(); LL | let x: &str = String::new();
| ---- ^^^^^^^^^^^^^ | ---- ^^^^^^^^^^^^^ expected `&str`, found `String`
| | | | |
| | expected `&str`, found `String`
| | help: consider borrowing here: `&String::new()`
| expected due to this | expected due to this
|
help: consider borrowing here
|
LL | let x: &str = &String::new();
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/coerce-suggestions.rs:12:10 --> $DIR/coerce-suggestions.rs:12:10

View File

@ -78,10 +78,12 @@ error[E0308]: mismatched types
--> $DIR/issue-39018.rs:29:17 --> $DIR/issue-39018.rs:29:17
| |
LL | let _ = a + b; LL | let _ = a + b;
| ^ | ^ expected `&str`, found `String`
| | |
| expected `&str`, found `String` help: consider borrowing here
| help: consider borrowing here: `&b` |
LL | let _ = a + &b;
| +
error[E0369]: cannot add `String` to `&String` error[E0369]: cannot add `String` to `&String`
--> $DIR/issue-39018.rs:30:15 --> $DIR/issue-39018.rs:30:15

View File

@ -10,10 +10,12 @@ error[E0308]: mismatched types
--> $DIR/str-array-assignment.rs:5:27 --> $DIR/str-array-assignment.rs:5:27
| |
LL | let u: &str = if true { s[..2] } else { s }; LL | let u: &str = if true { s[..2] } else { s };
| ^^^^^^ | ^^^^^^ expected `&str`, found `str`
| | |
| expected `&str`, found `str` help: consider borrowing here
| help: consider borrowing here: `&s[..2]` |
LL | let u: &str = if true { &s[..2] } else { s };
| +
error[E0277]: the size for values of type `str` cannot be known at compilation time error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/str-array-assignment.rs:7:7 --> $DIR/str-array-assignment.rs:7:7
@ -33,11 +35,14 @@ error[E0308]: mismatched types
--> $DIR/str-array-assignment.rs:9:17 --> $DIR/str-array-assignment.rs:9:17
| |
LL | let w: &str = s[..2]; LL | let w: &str = s[..2];
| ---- ^^^^^^ | ---- ^^^^^^ expected `&str`, found `str`
| | | | |
| | expected `&str`, found `str`
| | help: consider borrowing here: `&s[..2]`
| expected due to this | expected due to this
|
help: consider borrowing here
|
LL | let w: &str = &s[..2];
| +
error: aborting due to 4 previous errors error: aborting due to 4 previous errors

View File

@ -14,7 +14,7 @@ macro_rules! bla {
() => { () => {
x(123); x(123);
//~^ ERROR mismatched types //~^ ERROR mismatched types
//~| SUGGESTION &mut 123 //~| SUGGESTION &mut
}; };
($v:expr) => { ($v:expr) => {
x($v) x($v)
@ -25,5 +25,5 @@ fn main() {
bla!(); bla!();
bla!(456); bla!(456);
//~^ ERROR mismatched types //~^ ERROR mismatched types
//~| SUGGESTION &mut 456 //~| SUGGESTION &mut
} }

View File

@ -18,10 +18,8 @@ error[E0308]: mismatched types
--> $DIR/suggest-ref-macro.rs:15:11 --> $DIR/suggest-ref-macro.rs:15:11
| |
LL | x(123); LL | x(123);
| - ^^^ | - ^^^ expected `&mut i32`, found integer
| | | | |
| | expected `&mut i32`, found integer
| | help: consider mutably borrowing here: `&mut 123`
| arguments to this function are incorrect | arguments to this function are incorrect
... ...
LL | bla!(); LL | bla!();
@ -33,6 +31,10 @@ note: function defined here
LL | fn x(_: &mut i32) {} LL | fn x(_: &mut i32) {}
| ^ ----------- | ^ -----------
= note: this error originates in the macro `bla` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `bla` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider mutably borrowing here
|
LL | x(&mut 123);
| ++++
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/suggest-ref-macro.rs:26:10 --> $DIR/suggest-ref-macro.rs:26:10
@ -41,16 +43,17 @@ LL | x($v)
| - arguments to this function are incorrect | - arguments to this function are incorrect
... ...
LL | bla!(456); LL | bla!(456);
| ^^^ | ^^^ expected `&mut i32`, found integer
| |
| expected `&mut i32`, found integer
| help: consider mutably borrowing here: `&mut 456`
| |
note: function defined here note: function defined here
--> $DIR/suggest-ref-macro.rs:11:4 --> $DIR/suggest-ref-macro.rs:11:4
| |
LL | fn x(_: &mut i32) {} LL | fn x(_: &mut i32) {}
| ^ ----------- | ^ -----------
help: consider mutably borrowing here
|
LL | bla!(&mut 456);
| ++++
error: aborting due to 3 previous errors error: aborting due to 3 previous errors

View File

@ -378,10 +378,8 @@ error[E0308]: mismatched types
--> $DIR/type-mismatch.rs:47:23 --> $DIR/type-mismatch.rs:47:23
| |
LL | want::<&Foo<foo>>(f); LL | want::<&Foo<foo>>(f);
| ----------------- ^ | ----------------- ^ expected `&Foo<foo>`, found `Foo<foo>`
| | | | |
| | expected `&Foo<foo>`, found `Foo<foo>`
| | help: consider borrowing here: `&f`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected reference `&Foo<foo>` = note: expected reference `&Foo<foo>`
@ -391,6 +389,10 @@ note: function defined here
| |
LL | fn want<T>(t: T) {} LL | fn want<T>(t: T) {}
| ^^^^ ---- | ^^^^ ----
help: consider borrowing here
|
LL | want::<&Foo<foo>>(&f);
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/type-mismatch.rs:48:26 --> $DIR/type-mismatch.rs:48:26
@ -556,10 +558,8 @@ error[E0308]: mismatched types
--> $DIR/type-mismatch.rs:61:26 --> $DIR/type-mismatch.rs:61:26
| |
LL | want::<&Foo<foo, B>>(f); LL | want::<&Foo<foo, B>>(f);
| -------------------- ^ | -------------------- ^ expected `&Foo<foo, B>`, found `Foo<foo, B>`
| | | | |
| | expected `&Foo<foo, B>`, found `Foo<foo, B>`
| | help: consider borrowing here: `&f`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected reference `&Foo<foo, B>` = note: expected reference `&Foo<foo, B>`
@ -569,6 +569,10 @@ note: function defined here
| |
LL | fn want<T>(t: T) {} LL | fn want<T>(t: T) {}
| ^^^^ ---- | ^^^^ ----
help: consider borrowing here
|
LL | want::<&Foo<foo, B>>(&f);
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/type-mismatch.rs:65:19 --> $DIR/type-mismatch.rs:65:19

View File

@ -42,13 +42,14 @@ error[E0308]: mismatched types
LL | fn index<'a, K, V>(map: &'a HashMap<K, V>, k: K) -> &'a V { LL | fn index<'a, K, V>(map: &'a HashMap<K, V>, k: K) -> &'a V {
| - this type parameter | - this type parameter
LL | map[k] LL | map[k]
| ^ | ^ expected `&K`, found type parameter `K`
| |
| expected `&K`, found type parameter `K`
| help: consider borrowing here: `&k`
| |
= note: expected reference `&K` = note: expected reference `&K`
found type parameter `K` found type parameter `K`
help: consider borrowing here
|
LL | map[&k]
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/bad-index-due-to-nested.rs:20:5 --> $DIR/bad-index-due-to-nested.rs:20:5
@ -56,13 +57,14 @@ error[E0308]: mismatched types
LL | fn index<'a, K, V>(map: &'a HashMap<K, V>, k: K) -> &'a V { LL | fn index<'a, K, V>(map: &'a HashMap<K, V>, k: K) -> &'a V {
| - this type parameter ----- expected `&'a V` because of return type | - this type parameter ----- expected `&'a V` because of return type
LL | map[k] LL | map[k]
| ^^^^^^ | ^^^^^^ expected `&V`, found type parameter `V`
| |
| expected `&V`, found type parameter `V`
| help: consider borrowing here: `&map[k]`
| |
= note: expected reference `&'a V` = note: expected reference `&'a V`
found type parameter `V` found type parameter `V`
help: consider borrowing here
|
LL | &map[k]
| +
error: aborting due to 4 previous errors error: aborting due to 4 previous errors

View File

@ -2,16 +2,18 @@ error[E0308]: mismatched types
--> $DIR/bad-type-in-vec-contains.rs:5:21 --> $DIR/bad-type-in-vec-contains.rs:5:21
| |
LL | primes.contains(3); LL | primes.contains(3);
| -------- ^ | -------- ^ expected `&_`, found integer
| | | | |
| | expected `&_`, found integer
| | help: consider borrowing here: `&3`
| arguments to this method are incorrect | arguments to this method are incorrect
| |
= note: expected reference `&_` = note: expected reference `&_`
found type `{integer}` found type `{integer}`
note: method defined here note: method defined here
--> $SRC_DIR/core/src/slice/mod.rs:LL:COL --> $SRC_DIR/core/src/slice/mod.rs:LL:COL
help: consider borrowing here
|
LL | primes.contains(&3);
| +
error: aborting due to previous error error: aborting due to previous error

View File

@ -20,10 +20,8 @@ error[E0308]: mismatched types
--> $DIR/issue-13853.rs:37:13 --> $DIR/issue-13853.rs:37:13
| |
LL | iterate(graph); LL | iterate(graph);
| ------- ^^^^^ | ------- ^^^^^ expected `&_`, found `Vec<Stuff>`
| | | | |
| | expected `&_`, found `Vec<Stuff>`
| | help: consider borrowing here: `&graph`
| arguments to this function are incorrect | arguments to this function are incorrect
| |
= note: expected reference `&_` = note: expected reference `&_`
@ -33,6 +31,10 @@ note: function defined here
| |
LL | fn iterate<N: Node, G: Graph<N>>(graph: &G) { LL | fn iterate<N: Node, G: Graph<N>>(graph: &G) {
| ^^^^^^^ --------- | ^^^^^^^ ---------
help: consider borrowing here
|
LL | iterate(&graph);
| +
error: aborting due to 3 previous errors error: aborting due to 3 previous errors

View File

@ -16,11 +16,14 @@ error[E0308]: mismatched types
--> $DIR/suggest-borrow.rs:3:20 --> $DIR/suggest-borrow.rs:3:20
| |
LL | let x: &[u8] = vec!(1, 2, 3)[..]; LL | let x: &[u8] = vec!(1, 2, 3)[..];
| ----- ^^^^^^^^^^^^^^^^^ | ----- ^^^^^^^^^^^^^^^^^ expected `&[u8]`, found `[{integer}]`
| | | | |
| | expected `&[u8]`, found `[{integer}]`
| | help: consider borrowing here: `&vec!(1, 2, 3)[..]`
| expected due to this | expected due to this
|
help: consider borrowing here
|
LL | let x: &[u8] = &vec!(1, 2, 3)[..];
| +
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/suggest-borrow.rs:4:19 --> $DIR/suggest-borrow.rs:4:19