Skip to content

Commit

Permalink
Applying PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Dec 28, 2020
1 parent aa00c0e commit 1ff5e48
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
18 changes: 5 additions & 13 deletions clippy_lints/src/copies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ declare_clippy_lint! {
}

declare_clippy_lint! {
/// **What it does:** Checks if the `if` and `else` block contain shared that can be
/// **What it does:** Checks if the `if` and `else` block contain shared code that can be
/// moved out of the blocks.
///
/// **Why is this bad?** Duplicate code is less maintainable.
Expand Down Expand Up @@ -133,7 +133,7 @@ declare_clippy_lint! {
/// };
/// ```
pub SHARED_CODE_IN_IF_BLOCKS,
complexity,
pedantic,
"`if` statement with shared code in all blocks"
}

Expand Down Expand Up @@ -172,14 +172,6 @@ fn lint_same_then_else(cx: &LateContext<'_>, blocks: &[&Block<'_>], has_uncondit
}
}

fn min(a: usize, b: usize) -> usize {
if a < b {
a
} else {
b
}
}

fn lint_duplicate_code(cx: &LateContext<'_>, position: &str, lint_start: Span, lint_end: Span) {
span_lint_and_help(
cx,
Expand Down Expand Up @@ -232,9 +224,9 @@ fn lint_same_then_else(cx: &LateContext<'_>, blocks: &[&Block<'_>], has_uncondit
return;
}

start_eq = min(start_eq, current_start_eq);
end_eq = min(end_eq, current_end_eq);
expr_eq = expr_eq && block_expr_eq;
start_eq = start_eq.min(current_start_eq);
end_eq = end_eq.min(current_end_eq);
expr_eq &= block_expr_eq;

// We can return if the eq count is 0 from both sides or if it has no unconditional else case
if !has_unconditional_else || (start_eq == 0 && end_eq == 0 && !(eval_expr && expr_eq)) {
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/shared_code_in_if_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ fn shared_code_at_end() {
z
};

let _z = if x == 8 {
println!("Branch 1");
let mut z = 1;
z += 10;
foo!();
z
} else {
println!("Branch 2");
let mut z = 2;
z += 10;
foo!();
z
};

// Lint at start and end
let _ = if x == 1 {
println!("I'm the same as my brother branch");
Expand Down
19 changes: 15 additions & 4 deletions tests/ui/shared_code_in_if_blocks.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,19 @@ LL | | } else {
|
= help: Consider moving the code out of the if statement to prevent code duplication

error: All if blocks contain the same code at the end
--> $DIR/shared_code_in_if_blocks.rs:161:9
|
LL | / z += 10;
LL | | foo!();
LL | | z
LL | | } else {
| |_____^
|
= help: Consider moving the code out of the if statement to prevent code duplication

error: All if blocks contain the same code at the start
--> $DIR/shared_code_in_if_blocks.rs:159:23
--> $DIR/shared_code_in_if_blocks.rs:173:23
|
LL | let _ = if x == 1 {
| _______________________^
Expand All @@ -113,7 +124,7 @@ LL | | println!("I'm the same as my brother branch");
= help: Consider moving the code out of the if statement to prevent code duplication

error: All if blocks contain the same code at the end
--> $DIR/shared_code_in_if_blocks.rs:162:9
--> $DIR/shared_code_in_if_blocks.rs:176:9
|
LL | / println!("End of block");
LL | | false
Expand All @@ -123,13 +134,13 @@ LL | | } else {
= help: Consider moving the code out of the if statement to prevent code duplication

error: All if blocks contain the same code at the end
--> $DIR/shared_code_in_if_blocks.rs:192:9
--> $DIR/shared_code_in_if_blocks.rs:206:9
|
LL | / println!("We are doppelgänger")
LL | | } else {
| |_____^
|
= help: Consider moving the code out of the if statement to prevent code duplication

error: aborting due to 11 previous errors
error: aborting due to 12 previous errors

0 comments on commit 1ff5e48

Please sign in to comment.