Skip to content

Commit 04db249

Browse files
committed
Applying PR suggestions
* Update lints yay
1 parent aa00c0e commit 04db249

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

clippy_lints/src/copies.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ declare_clippy_lint! {
105105
}
106106

107107
declare_clippy_lint! {
108-
/// **What it does:** Checks if the `if` and `else` block contain shared that can be
108+
/// **What it does:** Checks if the `if` and `else` block contain shared code that can be
109109
/// moved out of the blocks.
110110
///
111111
/// **Why is this bad?** Duplicate code is less maintainable.
@@ -133,7 +133,7 @@ declare_clippy_lint! {
133133
/// };
134134
/// ```
135135
pub SHARED_CODE_IN_IF_BLOCKS,
136-
complexity,
136+
pedantic,
137137
"`if` statement with shared code in all blocks"
138138
}
139139

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

175-
fn min(a: usize, b: usize) -> usize {
176-
if a < b {
177-
a
178-
} else {
179-
b
180-
}
181-
}
182-
183175
fn lint_duplicate_code(cx: &LateContext<'_>, position: &str, lint_start: Span, lint_end: Span) {
184176
span_lint_and_help(
185177
cx,
@@ -232,9 +224,9 @@ fn lint_same_then_else(cx: &LateContext<'_>, blocks: &[&Block<'_>], has_uncondit
232224
return;
233225
}
234226

235-
start_eq = min(start_eq, current_start_eq);
236-
end_eq = min(end_eq, current_end_eq);
237-
expr_eq = expr_eq && block_expr_eq;
227+
start_eq = start_eq.min(current_start_eq);
228+
end_eq = end_eq.min(current_end_eq);
229+
expr_eq &= block_expr_eq;
238230

239231
// We can return if the eq count is 0 from both sides or if it has no unconditional else case
240232
if !has_unconditional_else || (start_eq == 0 && end_eq == 0 && !(eval_expr && expr_eq)) {

clippy_lints/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12681268
LintId::of(&bit_mask::VERBOSE_BIT_MASK),
12691269
LintId::of(&checked_conversions::CHECKED_CONVERSIONS),
12701270
LintId::of(&copies::SAME_FUNCTIONS_IN_IF_CONDITION),
1271+
LintId::of(&copies::SHARED_CODE_IN_IF_BLOCKS),
12711272
LintId::of(&copy_iterator::COPY_ITERATOR),
12721273
LintId::of(&default::DEFAULT_TRAIT_ACCESS),
12731274
LintId::of(&dereference::EXPLICIT_DEREF_METHODS),
@@ -1382,7 +1383,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13821383
LintId::of(&comparison_chain::COMPARISON_CHAIN),
13831384
LintId::of(&copies::IFS_SAME_COND),
13841385
LintId::of(&copies::IF_SAME_THEN_ELSE),
1385-
LintId::of(&copies::SHARED_CODE_IN_IF_BLOCKS),
13861386
LintId::of(&default::FIELD_REASSIGN_WITH_DEFAULT),
13871387
LintId::of(&derive::DERIVE_HASH_XOR_EQ),
13881388
LintId::of(&derive::DERIVE_ORD_XOR_PARTIAL_ORD),
@@ -1753,7 +1753,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
17531753
LintId::of(&assign_ops::MISREFACTORED_ASSIGN_OP),
17541754
LintId::of(&attrs::DEPRECATED_CFG_ATTR),
17551755
LintId::of(&booleans::NONMINIMAL_BOOL),
1756-
LintId::of(&copies::SHARED_CODE_IN_IF_BLOCKS),
17571756
LintId::of(&double_comparison::DOUBLE_COMPARISONS),
17581757
LintId::of(&double_parens::DOUBLE_PARENS),
17591758
LintId::of(&duration_subsec::DURATION_SUBSEC),

tests/ui/shared_code_in_if_blocks.rs

+14
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ fn shared_code_at_end() {
155155
z
156156
};
157157

158+
let _z = if x == 8 {
159+
println!("Branch 1");
160+
let mut z = 1;
161+
z += 10;
162+
foo!();
163+
z
164+
} else {
165+
println!("Branch 2");
166+
let mut z = 2;
167+
z += 10;
168+
foo!();
169+
z
170+
};
171+
158172
// Lint at start and end
159173
let _ = if x == 1 {
160174
println!("I'm the same as my brother branch");

tests/ui/shared_code_in_if_blocks.stderr

+15-4
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,19 @@ LL | | } else {
102102
|
103103
= help: Consider moving the code out of the if statement to prevent code duplication
104104

105+
error: All if blocks contain the same code at the end
106+
--> $DIR/shared_code_in_if_blocks.rs:161:9
107+
|
108+
LL | / z += 10;
109+
LL | | foo!();
110+
LL | | z
111+
LL | | } else {
112+
| |_____^
113+
|
114+
= help: Consider moving the code out of the if statement to prevent code duplication
115+
105116
error: All if blocks contain the same code at the start
106-
--> $DIR/shared_code_in_if_blocks.rs:159:23
117+
--> $DIR/shared_code_in_if_blocks.rs:173:23
107118
|
108119
LL | let _ = if x == 1 {
109120
| _______________________^
@@ -113,7 +124,7 @@ LL | | println!("I'm the same as my brother branch");
113124
= help: Consider moving the code out of the if statement to prevent code duplication
114125

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

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

134-
error: aborting due to 11 previous errors
145+
error: aborting due to 12 previous errors
135146

0 commit comments

Comments
 (0)