Skip to content

Commit 5e1d19d

Browse files
committed
Auto merge of #95433 - Dylan-DPC:rollup-xdfit9h, r=Dylan-DPC
Rollup of 4 pull requests Successful merges: - #94566 (Show ignore message in console and json output) - #95415 (diagnostics: regression test for HashMap iter_mut suggestion) - #95422 (Refactor: Use `format-args-capture` and remove an unnecessary nested block) - #95424 (:arrow_up: rust-analyzer) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 05d2221 + 7fdde18 commit 5e1d19d

File tree

6 files changed

+93
-35
lines changed

6 files changed

+93
-35
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
787787
_,
788788
[
789789
Expr {
790-
kind: MethodCall(path_segment, ..),
790+
kind:
791+
MethodCall(
792+
path_segment,
793+
_args,
794+
span,
795+
),
791796
hir_id,
792797
..
793798
},
@@ -831,7 +836,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
831836
if let Some(mut suggestions) = opt_suggestions {
832837
if suggestions.peek().is_some() {
833838
err.span_suggestions(
834-
path_segment.ident.span,
839+
*span,
835840
"use mutable method",
836841
suggestions,
837842
Applicability::MaybeIncorrect,

compiler/rustc_typeck/src/check/op.rs

+28-32
Original file line numberDiff line numberDiff line change
@@ -299,52 +299,52 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
299299
IsAssign::No => {
300300
let (message, missing_trait, use_output) = match op.node {
301301
hir::BinOpKind::Add => (
302-
format!("cannot add `{}` to `{}`", rhs_ty, lhs_ty),
302+
format!("cannot add `{rhs_ty}` to `{lhs_ty}`"),
303303
Some("std::ops::Add"),
304304
true,
305305
),
306306
hir::BinOpKind::Sub => (
307-
format!("cannot subtract `{}` from `{}`", rhs_ty, lhs_ty),
307+
format!("cannot subtract `{rhs_ty}` from `{lhs_ty}`"),
308308
Some("std::ops::Sub"),
309309
true,
310310
),
311311
hir::BinOpKind::Mul => (
312-
format!("cannot multiply `{}` by `{}`", lhs_ty, rhs_ty),
312+
format!("cannot multiply `{lhs_ty}` by `{rhs_ty}`"),
313313
Some("std::ops::Mul"),
314314
true,
315315
),
316316
hir::BinOpKind::Div => (
317-
format!("cannot divide `{}` by `{}`", lhs_ty, rhs_ty),
317+
format!("cannot divide `{lhs_ty}` by `{rhs_ty}`"),
318318
Some("std::ops::Div"),
319319
true,
320320
),
321321
hir::BinOpKind::Rem => (
322-
format!("cannot mod `{}` by `{}`", lhs_ty, rhs_ty),
322+
format!("cannot mod `{lhs_ty}` by `{rhs_ty}`"),
323323
Some("std::ops::Rem"),
324324
true,
325325
),
326326
hir::BinOpKind::BitAnd => (
327-
format!("no implementation for `{} & {}`", lhs_ty, rhs_ty),
327+
format!("no implementation for `{lhs_ty} & {rhs_ty}`"),
328328
Some("std::ops::BitAnd"),
329329
true,
330330
),
331331
hir::BinOpKind::BitXor => (
332-
format!("no implementation for `{} ^ {}`", lhs_ty, rhs_ty),
332+
format!("no implementation for `{lhs_ty} ^ {rhs_ty}`"),
333333
Some("std::ops::BitXor"),
334334
true,
335335
),
336336
hir::BinOpKind::BitOr => (
337-
format!("no implementation for `{} | {}`", lhs_ty, rhs_ty),
337+
format!("no implementation for `{lhs_ty} | {rhs_ty}`"),
338338
Some("std::ops::BitOr"),
339339
true,
340340
),
341341
hir::BinOpKind::Shl => (
342-
format!("no implementation for `{} << {}`", lhs_ty, rhs_ty),
342+
format!("no implementation for `{lhs_ty} << {rhs_ty}`"),
343343
Some("std::ops::Shl"),
344344
true,
345345
),
346346
hir::BinOpKind::Shr => (
347-
format!("no implementation for `{} >> {}`", lhs_ty, rhs_ty),
347+
format!("no implementation for `{lhs_ty} >> {rhs_ty}`"),
348348
Some("std::ops::Shr"),
349349
true,
350350
),
@@ -477,8 +477,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
477477
// When we know that a missing bound is responsible, we don't show
478478
// this note as it is redundant.
479479
err.note(&format!(
480-
"the trait `{}` is not implemented for `{}`",
481-
missing_trait, lhs_ty
480+
"the trait `{missing_trait}` is not implemented for `{lhs_ty}`"
482481
));
483482
}
484483
} else {
@@ -679,19 +678,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
679678
};
680679
let mut visitor = TypeParamVisitor(vec![]);
681680
visitor.visit_ty(operand_ty);
682-
if let [ty] = &visitor.0[..] {
683-
if let ty::Param(p) = *operand_ty.kind() {
684-
suggest_constraining_param(
685-
self.tcx,
686-
self.body_id,
687-
&mut err,
688-
*ty,
689-
operand_ty,
690-
missing_trait,
691-
p,
692-
true,
693-
);
694-
}
681+
if let [ty] = &visitor.0[..] && let ty::Param(p) = *operand_ty.kind() {
682+
suggest_constraining_param(
683+
self.tcx,
684+
self.body_id,
685+
&mut err,
686+
*ty,
687+
operand_ty,
688+
missing_trait,
689+
p,
690+
true,
691+
);
695692
}
696693

697694
let sp = self.tcx.sess.source_map().start_point(ex.span);
@@ -722,10 +719,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
722719
err.span_suggestion(
723720
ex.span,
724721
&format!(
725-
"you may have meant the maximum value of `{}`",
726-
actual
722+
"you may have meant the maximum value of `{actual}`",
727723
),
728-
format!("{}::MAX", actual),
724+
format!("{actual}::MAX"),
729725
Applicability::MaybeIncorrect,
730726
);
731727
}
@@ -988,7 +984,7 @@ fn suggest_constraining_param(
988984
set_output: bool,
989985
) {
990986
let hir = tcx.hir();
991-
let msg = &format!("`{}` might need a bound for `{}`", lhs_ty, missing_trait);
987+
let msg = &format!("`{lhs_ty}` might need a bound for `{missing_trait}`");
992988
// Try to find the def-id and details for the parameter p. We have only the index,
993989
// so we have to find the enclosing function's def-id, then look through its declared
994990
// generic parameters to get the declaration.
@@ -1002,13 +998,13 @@ fn suggest_constraining_param(
1002998
.as_ref()
1003999
.and_then(|node| node.generics())
10041000
{
1005-
let output = if set_output { format!("<Output = {}>", rhs_ty) } else { String::new() };
1001+
let output = if set_output { format!("<Output = {rhs_ty}>") } else { String::new() };
10061002
suggest_constraining_type_param(
10071003
tcx,
10081004
generics,
10091005
&mut err,
1010-
&format!("{}", lhs_ty),
1011-
&format!("{}{}", missing_trait, output),
1006+
&lhs_ty.to_string(),
1007+
&format!("{missing_trait}{output}"),
10121008
None,
10131009
);
10141010
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// run-rustfix
2+
// https://github.com/rust-lang/rust/issues/82081
3+
4+
use std::collections::HashMap;
5+
6+
struct Test {
7+
v: u32,
8+
}
9+
10+
fn main() {
11+
let mut map = HashMap::new();
12+
map.insert("a", Test { v: 0 });
13+
14+
for (_k, mut v) in map.iter_mut() {
15+
//~^ HELP use mutable method
16+
//~| NOTE this iterator yields `&` references
17+
v.v += 1;
18+
//~^ ERROR cannot assign to `v.v`
19+
//~| NOTE `v` is a `&` reference
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// run-rustfix
2+
// https://github.com/rust-lang/rust/issues/82081
3+
4+
use std::collections::HashMap;
5+
6+
struct Test {
7+
v: u32,
8+
}
9+
10+
fn main() {
11+
let mut map = HashMap::new();
12+
map.insert("a", Test { v: 0 });
13+
14+
for (_k, mut v) in map.iter() {
15+
//~^ HELP use mutable method
16+
//~| NOTE this iterator yields `&` references
17+
v.v += 1;
18+
//~^ ERROR cannot assign to `v.v`
19+
//~| NOTE `v` is a `&` reference
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0594]: cannot assign to `v.v`, which is behind a `&` reference
2+
--> $DIR/suggest-mut-method-for-loop-hashmap.rs:17:9
3+
|
4+
LL | for (_k, mut v) in map.iter() {
5+
| ----------
6+
| | |
7+
| | help: use mutable method: `iter_mut()`
8+
| this iterator yields `&` references
9+
...
10+
LL | v.v += 1;
11+
| ^^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0594`.

src/tools/rust-analyzer

0 commit comments

Comments
 (0)