Skip to content

Commit c0e4817

Browse files
committed
Remove logic duplication
1 parent 42d7174 commit c0e4817

16 files changed

+37
-52
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -380,61 +380,46 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
380380
let typeck = self.infcx.tcx.typeck(self.mir_def_id());
381381
let hir_id = hir.get_parent_node(expr.hir_id);
382382
if let Some(parent) = hir.find(hir_id) {
383-
if let hir::Node::Expr(parent_expr) = parent
383+
let (def_id, args, offset) = if let hir::Node::Expr(parent_expr) = parent
384384
&& let hir::ExprKind::MethodCall(_, _, args, _) = parent_expr.kind
385385
&& let Some(def_id) = typeck.type_dependent_def_id(parent_expr.hir_id)
386-
&& let Some(def_id) = def_id.as_local()
387-
&& let Some(node) = hir.find(hir.local_def_id_to_hir_id(def_id))
388-
&& let Some(fn_sig) = node.fn_sig()
389-
&& let Some(ident) = node.ident()
390-
&& let Some(pos) = args.iter()
391-
.position(|arg| arg.hir_id == expr.hir_id)
392-
&& let Some(arg) = fn_sig.decl.inputs.get(pos + 1)
393386
{
394-
let mut span: MultiSpan = arg.span.into();
395-
span.push_span_label(
396-
arg.span,
397-
"this type parameter takes ownership of the value".to_string(),
398-
);
399-
span.push_span_label(
400-
ident.span,
401-
"in this method".to_string(),
402-
);
403-
err.span_note(
404-
span,
405-
format!(
406-
"consider changing this parameter type in `{}` to borrow instead \
407-
if ownering the value isn't necessary",
408-
ident,
409-
),
410-
);
411-
}
412-
if let hir::Node::Expr(parent_expr) = parent
387+
(def_id.as_local(), args, 1)
388+
} else if let hir::Node::Expr(parent_expr) = parent
413389
&& let hir::ExprKind::Call(call, args) = parent_expr.kind
414390
&& let ty::FnDef(def_id, _) = typeck.node_type(call.hir_id).kind()
415-
&& let Some(def_id) = def_id.as_local()
391+
{
392+
(def_id.as_local(), args, 0)
393+
} else {
394+
(None, &[][..], 0)
395+
};
396+
if let Some(def_id) = def_id
416397
&& let Some(node) = hir.find(hir.local_def_id_to_hir_id(def_id))
417398
&& let Some(fn_sig) = node.fn_sig()
418399
&& let Some(ident) = node.ident()
419400
&& let Some(pos) = args.iter()
420401
.position(|arg| arg.hir_id == expr.hir_id)
421-
&& let Some(arg) = fn_sig.decl.inputs.get(pos)
402+
&& let Some(arg) = fn_sig.decl.inputs.get(pos + offset)
422403
{
423404
let mut span: MultiSpan = arg.span.into();
424405
span.push_span_label(
425406
arg.span,
426407
"this type parameter takes ownership of the value".to_string(),
427408
);
409+
let descr = match node.fn_kind() {
410+
Some(hir::intravisit::FnKind::ItemFn(..)) | None => "function",
411+
Some(hir::intravisit::FnKind::Method(..)) => "method",
412+
Some(hir::intravisit::FnKind::Closure) => "closure",
413+
};
428414
span.push_span_label(
429415
ident.span,
430-
"in this function".to_string(),
416+
format!("in this {descr}"),
431417
);
432418
err.span_note(
433419
span,
434420
format!(
435-
"consider changing this parameter type in `{}` to borrow instead \
436-
if ownering the value isn't necessary",
437-
ident,
421+
"consider changing this parameter type in {descr} `{ident}` to \
422+
borrow instead if ownering the value isn't necessary",
438423
),
439424
);
440425
}

src/test/ui/borrowck/borrowck-consume-unsize-vec.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | consume(b);
88
LL | consume(b);
99
| ^ value used here after move
1010
|
11-
note: consider changing this parameter type in `consume` to borrow instead if ownering the value isn't necessary
11+
note: consider changing this parameter type in function `consume` to borrow instead if ownering the value isn't necessary
1212
--> $DIR/borrowck-consume-unsize-vec.rs:3:15
1313
|
1414
LL | fn consume(_: Box<[i32]>) {

src/test/ui/borrowck/borrowck-consume-upcast-box.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | consume(b);
88
LL | consume(b);
99
| ^ value used here after move
1010
|
11-
note: consider changing this parameter type in `consume` to borrow instead if ownering the value isn't necessary
11+
note: consider changing this parameter type in function `consume` to borrow instead if ownering the value isn't necessary
1212
--> $DIR/borrowck-consume-upcast-box.rs:5:15
1313
|
1414
LL | fn consume(_: Box<dyn Foo>) {

src/test/ui/borrowck/mut-borrow-in-loop-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | for _ in 0..3 {
88
LL | Other::handle(value);
99
| ^^^^^ value moved here, in previous iteration of loop
1010
|
11-
note: consider changing this parameter type in `handle` to borrow instead if ownering the value isn't necessary
11+
note: consider changing this parameter type in function `handle` to borrow instead if ownering the value isn't necessary
1212
--> $DIR/mut-borrow-in-loop-2.rs:9:22
1313
|
1414
LL | fn handle(value: T) -> Self;

src/test/ui/liveness/liveness-move-call-arg.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | loop {
99
LL | take(x);
1010
| ^ value moved here, in previous iteration of loop
1111
|
12-
note: consider changing this parameter type in `take` to borrow instead if ownering the value isn't necessary
12+
note: consider changing this parameter type in function `take` to borrow instead if ownering the value isn't necessary
1313
--> $DIR/liveness-move-call-arg.rs:1:13
1414
|
1515
LL | fn take(_x: Box<isize>) {}

src/test/ui/liveness/liveness-use-after-send.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | send(ch, message);
88
LL | println!("{}", message);
99
| ^^^^^^^ value borrowed here after move
1010
|
11-
note: consider changing this parameter type in `send` to borrow instead if ownering the value isn't necessary
11+
note: consider changing this parameter type in function `send` to borrow instead if ownering the value isn't necessary
1212
--> $DIR/liveness-use-after-send.rs:3:54
1313
|
1414
LL | fn send<T:Send + std::fmt::Debug>(ch: Chan<T>, data: T) {

src/test/ui/moves/borrow-closures-instead-of-move.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | loop {
88
LL | takes_fnonce(f);
99
| ^ value moved here, in previous iteration of loop
1010
|
11-
note: consider changing this parameter type in `takes_fnonce` to borrow instead if ownering the value isn't necessary
11+
note: consider changing this parameter type in function `takes_fnonce` to borrow instead if ownering the value isn't necessary
1212
--> $DIR/borrow-closures-instead-of-move.rs:34:20
1313
|
1414
LL | fn takes_fnonce(_: impl FnOnce()) {}
@@ -32,7 +32,7 @@ LL | takes_fnonce(m);
3232
LL | takes_fnonce(m);
3333
| ^ value used here after move
3434
|
35-
note: consider changing this parameter type in `takes_fnonce` to borrow instead if ownering the value isn't necessary
35+
note: consider changing this parameter type in function `takes_fnonce` to borrow instead if ownering the value isn't necessary
3636
--> $DIR/borrow-closures-instead-of-move.rs:34:20
3737
|
3838
LL | fn takes_fnonce(_: impl FnOnce()) {}
@@ -58,7 +58,7 @@ note: closure cannot be moved more than once as it is not `Copy` due to moving t
5858
|
5959
LL | x += 1;
6060
| ^
61-
note: consider changing this parameter type in `takes_fnonce` to borrow instead if ownering the value isn't necessary
61+
note: consider changing this parameter type in function `takes_fnonce` to borrow instead if ownering the value isn't necessary
6262
--> $DIR/borrow-closures-instead-of-move.rs:34:20
6363
|
6464
LL | fn takes_fnonce(_: impl FnOnce()) {}

src/test/ui/moves/move-guard-same-consts.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | (1, 2) if take(x) => (),
99
LL | (1, 2) if take(x) => (),
1010
| ^ value used here after move
1111
|
12-
note: consider changing this parameter type in `take` to borrow instead if ownering the value isn't necessary
12+
note: consider changing this parameter type in function `take` to borrow instead if ownering the value isn't necessary
1313
--> $DIR/move-guard-same-consts.rs:25:15
1414
|
1515
LL | fn take<T>(_: T) -> bool { false }

src/test/ui/moves/move-in-guard-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | (1, _) if take(x) => (),
99
LL | (_, 2) if take(x) => (),
1010
| ^ value used here after move
1111
|
12-
note: consider changing this parameter type in `take` to borrow instead if ownering the value isn't necessary
12+
note: consider changing this parameter type in function `take` to borrow instead if ownering the value isn't necessary
1313
--> $DIR/move-in-guard-1.rs:15:15
1414
|
1515
LL | fn take<T>(_: T) -> bool { false }

src/test/ui/moves/move-in-guard-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let x: Box<_> = Box::new(1);
77
LL | (_, 2) if take(x) => (),
88
| ^ value used here after move
99
|
10-
note: consider changing this parameter type in `take` to borrow instead if ownering the value isn't necessary
10+
note: consider changing this parameter type in function `take` to borrow instead if ownering the value isn't necessary
1111
--> $DIR/move-in-guard-2.rs:13:15
1212
|
1313
LL | fn take<T>(_: T) -> bool { false }

src/test/ui/moves/moves-based-on-type-exprs.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ LL | _ if guard(x) => 10,
108108
LL | touch(&x);
109109
| ^^ value borrowed here after move
110110
|
111-
note: consider changing this parameter type in `guard` to borrow instead if ownering the value isn't necessary
111+
note: consider changing this parameter type in function `guard` to borrow instead if ownering the value isn't necessary
112112
--> $DIR/moves-based-on-type-exprs.rs:6:14
113113
|
114114
LL | fn guard(_s: String) -> bool {panic!()}

src/test/ui/union/union-move.mirunsafeck.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | move_out(x.f1_nocopy);
99
LL | move_out(x.f2_nocopy);
1010
| ^^^^^^^^^^^ value used here after move
1111
|
12-
note: consider changing this parameter type in `move_out` to borrow instead if ownering the value isn't necessary
12+
note: consider changing this parameter type in function `move_out` to borrow instead if ownering the value isn't necessary
1313
--> $DIR/union-move.rs:10:19
1414
|
1515
LL | fn move_out<T>(x: T) {}
@@ -28,7 +28,7 @@ LL | move_out(x.f2_nocopy);
2828
LL | move_out(x.f3_copy);
2929
| ^^^^^^^^^ value used here after move
3030
|
31-
note: consider changing this parameter type in `move_out` to borrow instead if ownering the value isn't necessary
31+
note: consider changing this parameter type in function `move_out` to borrow instead if ownering the value isn't necessary
3232
--> $DIR/union-move.rs:10:19
3333
|
3434
LL | fn move_out<T>(x: T) {}

src/test/ui/union/union-move.thirunsafeck.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | move_out(x.f1_nocopy);
99
LL | move_out(x.f2_nocopy);
1010
| ^^^^^^^^^^^ value used here after move
1111
|
12-
note: consider changing this parameter type in `move_out` to borrow instead if ownering the value isn't necessary
12+
note: consider changing this parameter type in function `move_out` to borrow instead if ownering the value isn't necessary
1313
--> $DIR/union-move.rs:10:19
1414
|
1515
LL | fn move_out<T>(x: T) {}
@@ -28,7 +28,7 @@ LL | move_out(x.f2_nocopy);
2828
LL | move_out(x.f3_copy);
2929
| ^^^^^^^^^ value used here after move
3030
|
31-
note: consider changing this parameter type in `move_out` to borrow instead if ownering the value isn't necessary
31+
note: consider changing this parameter type in function `move_out` to borrow instead if ownering the value isn't necessary
3232
--> $DIR/union-move.rs:10:19
3333
|
3434
LL | fn move_out<T>(x: T) {}

src/test/ui/unsized-locals/borrow-after-move.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LL | drop_unsized(y);
2929
LL | println!("{}", &y);
3030
| ^^ value borrowed here after move
3131
|
32-
note: consider changing this parameter type in `drop_unsized` to borrow instead if ownering the value isn't necessary
32+
note: consider changing this parameter type in function `drop_unsized` to borrow instead if ownering the value isn't necessary
3333
--> $DIR/borrow-after-move.rs:14:31
3434
|
3535
LL | fn drop_unsized<T: ?Sized>(_: T) {}

src/test/ui/unsized-locals/double-move.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | drop_unsized(y);
1717
LL | drop_unsized(y);
1818
| ^ value used here after move
1919
|
20-
note: consider changing this parameter type in `drop_unsized` to borrow instead if ownering the value isn't necessary
20+
note: consider changing this parameter type in function `drop_unsized` to borrow instead if ownering the value isn't necessary
2121
--> $DIR/double-move.rs:14:31
2222
|
2323
LL | fn drop_unsized<T: ?Sized>(_: T) {}

src/test/ui/use/use-after-move-implicity-coerced-object.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL |
1010
LL | let x = n.to_string();
1111
| ^^^^^^^^^^^^^ value borrowed here after move
1212
|
13-
note: consider changing this parameter type in `push` to borrow instead if ownering the value isn't necessary
13+
note: consider changing this parameter type in method `push` to borrow instead if ownering the value isn't necessary
1414
--> $DIR/use-after-move-implicity-coerced-object.rs:17:27
1515
|
1616
LL | fn push(&mut self, n: Box<dyn ToString + 'static>) {

0 commit comments

Comments
 (0)