Skip to content

Commit aa00c0e

Browse files
committed
Finishing touches for the shared_code_in_if_blocks lint
1 parent 59a9fcb commit aa00c0e

File tree

4 files changed

+41
-63
lines changed

4 files changed

+41
-63
lines changed

clippy_lints/src/copies.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,9 @@ fn lint_same_then_else(cx: &LateContext<'_>, blocks: &[&Block<'_>], has_uncondit
196196
return;
197197
}
198198

199-
let eval_expr = if let Some(expr) = blocks[0].expr {
200-
cx.typeck_results().expr_ty(expr).is_unit()
201-
} else {
202-
false
203-
};
199+
let eval_expr = blocks[0]
200+
.expr
201+
.map_or(false, |expr| cx.typeck_results().expr_ty(expr).is_unit());
204202

205203
// Check if each block has shared code
206204
let mut start_eq = usize::MAX;
@@ -215,7 +213,7 @@ fn lint_same_then_else(cx: &LateContext<'_>, blocks: &[&Block<'_>], has_uncondit
215213
let current_end_eq = count_eq(&mut l_stmts.iter().rev(), &mut r_stmts.iter().rev(), |l, r| {
216214
evaluator.eq_stmt(l, r)
217215
});
218-
216+
219217
let block_expr_eq = both(&win[0].expr, &win[1].expr, |l, r| evaluator.eq_expr(l, r));
220218

221219
// IF_SAME_THEN_ELSE
@@ -262,7 +260,7 @@ fn lint_same_then_else(cx: &LateContext<'_>, blocks: &[&Block<'_>], has_uncondit
262260
if end_eq != 0 && (!eval_expr || expr_eq) {
263261
let index = first_block.stmts.len() - end_eq;
264262
let start = get_source_span(first_block.stmts[index].span);
265-
263+
266264
lint_duplicate_code(cx, "end", start, lint_end);
267265
} else if eval_expr && expr_eq {
268266
if let Some(expr) = first_block.expr {

clippy_lints/src/literal_representation.rs

+18-22
Original file line numberDiff line numberDiff line change
@@ -463,32 +463,28 @@ impl DecimalLiteralRepresentation {
463463
{
464464
return Err(WarningType::DecimalRepresentation);
465465
}
466-
} else if digits.len() < 4 {
467-
// Lint for Literals with a hex-representation of 2 or 3 digits
468-
let f = &digits[0..1]; // first digit
469-
let s = &digits[1..]; // suffix
470-
471-
// Powers of 2
472-
if ((f.eq("1") || f.eq("2") || f.eq("4") || f.eq("8")) && s.chars().all(|c| c == '0'))
473-
// Powers of 2 minus 1
474-
|| ((f.eq("1") || f.eq("3") || f.eq("7") || f.eq("F")) && s.chars().all(|c| c == 'F'))
475-
{
476-
return Err(WarningType::DecimalRepresentation);
477-
}
478466
} else {
479-
// Lint for Literals with a hex-representation of 4 digits or more
480467
let f = &digits[0..1]; // first digit
481468
let m = &digits[1..digits.len() - 1]; // middle digits, except last
482469
let s = &digits[1..]; // suffix
483-
484-
// Powers of 2 with a margin of +15/-16
485-
if ((f.eq("1") || f.eq("2") || f.eq("4") || f.eq("8")) && m.chars().all(|c| c == '0'))
486-
|| ((f.eq("1") || f.eq("3") || f.eq("7") || f.eq("F")) && m.chars().all(|c| c == 'F'))
487-
// Lint for representations with only 0s and Fs, while allowing 7 as the first
488-
// digit
489-
|| ((f.eq("7") || f.eq("F")) && s.chars().all(|c| c == '0' || c == 'F'))
490-
{
491-
return Err(WarningType::DecimalRepresentation);
470+
if digits.len() < 4 {
471+
// Powers of 2
472+
if ((f.eq("1") || f.eq("2") || f.eq("4") || f.eq("8")) && s.chars().all(|c| c == '0'))
473+
// Powers of 2 minus 1
474+
|| ((f.eq("1") || f.eq("3") || f.eq("7") || f.eq("F")) && s.chars().all(|c| c == 'F'))
475+
{
476+
return Err(WarningType::DecimalRepresentation);
477+
}
478+
} else {
479+
// Powers of 2 with a margin of +15/-16
480+
if ((f.eq("1") || f.eq("2") || f.eq("4") || f.eq("8")) && m.chars().all(|c| c == '0'))
481+
|| ((f.eq("1") || f.eq("3") || f.eq("7") || f.eq("F")) && m.chars().all(|c| c == 'F'))
482+
// Lint for representations with only 0s and Fs, while allowing 7 as the first
483+
// digit
484+
|| ((f.eq("7") || f.eq("F")) && s.chars().all(|c| c == '0' || c == 'F'))
485+
{
486+
return Err(WarningType::DecimalRepresentation);
487+
}
492488
}
493489
}
494490

clippy_lints/src/methods/manual_saturating_arithmetic.rs

+16-32
Original file line numberDiff line numberDiff line change
@@ -39,44 +39,28 @@ pub fn lint(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[&[hir::Expr<'_>]
3939
// "mul" is omitted because lhs can be negative.
4040
_ => return,
4141
}
42-
43-
let mut applicability = Applicability::MachineApplicable;
44-
span_lint_and_sugg(
45-
cx,
46-
super::MANUAL_SATURATING_ARITHMETIC,
47-
expr.span,
48-
"manual saturating arithmetic",
49-
&format!("try using `saturating_{}`", arith),
50-
format!(
51-
"{}.saturating_{}({})",
52-
snippet_with_applicability(cx, arith_lhs.span, "..", &mut applicability),
53-
arith,
54-
snippet_with_applicability(cx, arith_rhs.span, "..", &mut applicability),
55-
),
56-
applicability,
57-
);
5842
} else {
5943
match (mm, arith) {
6044
(MinMax::Max, "add" | "mul") | (MinMax::Min, "sub") => (),
6145
_ => return,
6246
}
63-
64-
let mut applicability = Applicability::MachineApplicable;
65-
span_lint_and_sugg(
66-
cx,
67-
super::MANUAL_SATURATING_ARITHMETIC,
68-
expr.span,
69-
"manual saturating arithmetic",
70-
&format!("try using `saturating_{}`", arith),
71-
format!(
72-
"{}.saturating_{}({})",
73-
snippet_with_applicability(cx, arith_lhs.span, "..", &mut applicability),
74-
arith,
75-
snippet_with_applicability(cx, arith_rhs.span, "..", &mut applicability),
76-
),
77-
applicability,
78-
);
7947
}
48+
49+
let mut applicability = Applicability::MachineApplicable;
50+
span_lint_and_sugg(
51+
cx,
52+
super::MANUAL_SATURATING_ARITHMETIC,
53+
expr.span,
54+
"manual saturating arithmetic",
55+
&format!("try using `saturating_{}`", arith),
56+
format!(
57+
"{}.saturating_{}({})",
58+
snippet_with_applicability(cx, arith_lhs.span, "..", &mut applicability),
59+
arith,
60+
snippet_with_applicability(cx, arith_rhs.span, "..", &mut applicability),
61+
),
62+
applicability,
63+
);
8064
}
8165

8266
#[derive(PartialEq, Eq)]

tests/ui/shared_code_in_if_blocks.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,11 @@ fn expr_with_unit_type() {
177177
if x == 9 {
178178
let _a = 17;
179179
let _b = 49;
180-
180+
181181
println!("A message")
182182
} else {
183183
let _b = 49;
184-
184+
185185
println!("A different message message")
186186
}
187187

0 commit comments

Comments
 (0)