Skip to content

Commit

Permalink
Applying PR suggestions
Browse files Browse the repository at this point in the history
* Update lints yay
  • Loading branch information
xFrednet committed Dec 28, 2020
1 parent aa00c0e commit 04db249
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 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
3 changes: 1 addition & 2 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&bit_mask::VERBOSE_BIT_MASK),
LintId::of(&checked_conversions::CHECKED_CONVERSIONS),
LintId::of(&copies::SAME_FUNCTIONS_IN_IF_CONDITION),
LintId::of(&copies::SHARED_CODE_IN_IF_BLOCKS),
LintId::of(&copy_iterator::COPY_ITERATOR),
LintId::of(&default::DEFAULT_TRAIT_ACCESS),
LintId::of(&dereference::EXPLICIT_DEREF_METHODS),
Expand Down Expand Up @@ -1382,7 +1383,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&comparison_chain::COMPARISON_CHAIN),
LintId::of(&copies::IFS_SAME_COND),
LintId::of(&copies::IF_SAME_THEN_ELSE),
LintId::of(&copies::SHARED_CODE_IN_IF_BLOCKS),
LintId::of(&default::FIELD_REASSIGN_WITH_DEFAULT),
LintId::of(&derive::DERIVE_HASH_XOR_EQ),
LintId::of(&derive::DERIVE_ORD_XOR_PARTIAL_ORD),
Expand Down Expand Up @@ -1753,7 +1753,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&assign_ops::MISREFACTORED_ASSIGN_OP),
LintId::of(&attrs::DEPRECATED_CFG_ATTR),
LintId::of(&booleans::NONMINIMAL_BOOL),
LintId::of(&copies::SHARED_CODE_IN_IF_BLOCKS),
LintId::of(&double_comparison::DOUBLE_COMPARISONS),
LintId::of(&double_parens::DOUBLE_PARENS),
LintId::of(&duration_subsec::DURATION_SUBSEC),
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 04db249

Please sign in to comment.