Skip to content

Commit 6064822

Browse files
authored
Fix branches_sharing_code suggests misleadingly when in assignment (#15076)
The lint will note `the end suggestion probably needs some adjustments to use the expression result correctly` when the expr's is not unit. So I extend this note to also appear when the expr is in an assignment. changelog: [`branches_sharing_code`] fix misleading suggestions when in assignment
2 parents 2c1c746 + 77ef3d7 commit 6064822

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

clippy_lints/src/copies.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use clippy_utils::{
1111
use core::iter;
1212
use core::ops::ControlFlow;
1313
use rustc_errors::Applicability;
14-
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, HirId, HirIdSet, Stmt, StmtKind, intravisit};
14+
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, HirId, HirIdSet, LetStmt, Node, Stmt, StmtKind, intravisit};
1515
use rustc_lint::{LateContext, LateLintPass};
1616
use rustc_middle::ty::TyCtxt;
1717
use rustc_session::impl_lint_pass;
@@ -295,7 +295,7 @@ fn lint_branches_sharing_code<'tcx>(
295295
sugg,
296296
Applicability::Unspecified,
297297
);
298-
if !cx.typeck_results().expr_ty(expr).is_unit() {
298+
if is_expr_parent_assignment(cx, expr) || !cx.typeck_results().expr_ty(expr).is_unit() {
299299
diag.note("the end suggestion probably needs some adjustments to use the expression result correctly");
300300
}
301301
}
@@ -660,3 +660,17 @@ fn lint_same_fns_in_if_cond(cx: &LateContext<'_>, conds: &[&Expr<'_>]) {
660660
);
661661
}
662662
}
663+
664+
fn is_expr_parent_assignment(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
665+
let parent = cx.tcx.parent_hir_node(expr.hir_id);
666+
if let Node::LetStmt(LetStmt { init: Some(e), .. })
667+
| Node::Expr(Expr {
668+
kind: ExprKind::Assign(_, e, _),
669+
..
670+
}) = parent
671+
{
672+
return e.hir_id == expr.hir_id;
673+
}
674+
675+
false
676+
}

tests/ui/branches_sharing_code/shared_at_bottom.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,27 @@ mod issue14873 {
276276
}
277277
}
278278
}
279+
280+
fn issue15004() {
281+
let a = 12u32;
282+
let b = 13u32;
283+
let mut c = 8u32;
284+
285+
let mut result = if b > a {
286+
c += 1;
287+
0
288+
} else {
289+
c += 2;
290+
0
291+
//~^ branches_sharing_code
292+
};
293+
294+
result = if b > a {
295+
c += 1;
296+
1
297+
} else {
298+
c += 2;
299+
1
300+
//~^ branches_sharing_code
301+
};
302+
}

tests/ui/branches_sharing_code/shared_at_bottom.stderr

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,35 @@ LL ~ }
172172
LL + let y = 1;
173173
|
174174

175-
error: aborting due to 10 previous errors
175+
error: all if blocks contain the same code at the end
176+
--> tests/ui/branches_sharing_code/shared_at_bottom.rs:290:5
177+
|
178+
LL | / 0
179+
LL | |
180+
LL | | };
181+
| |_____^
182+
|
183+
= note: the end suggestion probably needs some adjustments to use the expression result correctly
184+
help: consider moving these statements after the if
185+
|
186+
LL ~ }
187+
LL ~ 0;
188+
|
189+
190+
error: all if blocks contain the same code at the end
191+
--> tests/ui/branches_sharing_code/shared_at_bottom.rs:299:5
192+
|
193+
LL | / 1
194+
LL | |
195+
LL | | };
196+
| |_____^
197+
|
198+
= note: the end suggestion probably needs some adjustments to use the expression result correctly
199+
help: consider moving these statements after the if
200+
|
201+
LL ~ }
202+
LL ~ 1;
203+
|
204+
205+
error: aborting due to 12 previous errors
176206

0 commit comments

Comments
 (0)