Skip to content

Commit 617c65b

Browse files
committed
Moving shared_code_in_if_blocks to clippy::complexity and running lintcheck
1 parent c74e49e commit 617c65b

21 files changed

+180
-84
lines changed

clippy_lints/src/copies.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ declare_clippy_lint! {
142142
/// };
143143
/// ```
144144
pub SHARED_CODE_IN_IF_BLOCKS,
145-
nursery,
145+
complexity,
146146
"`if` statement with shared code in all blocks"
147147
}
148148

@@ -457,11 +457,11 @@ fn emit_shared_code_in_if_blocks_lint(
457457

458458
let add_optional_msgs = |diag: &mut DiagnosticBuilder<'_>| {
459459
if add_expr_note {
460-
diag.note("The end suggestion probably needs some adjustments to use the expression result correctly.");
460+
diag.note("The end suggestion probably needs some adjustments to use the expression result correctly");
461461
}
462462

463463
if warn_about_moved_symbol {
464-
diag.warn("Some moved values might need to be renamed to avoid wrong references.");
464+
diag.warn("Some moved values might need to be renamed to avoid wrong references");
465465
}
466466
};
467467

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14851485
LintId::of(&comparison_chain::COMPARISON_CHAIN),
14861486
LintId::of(&copies::IFS_SAME_COND),
14871487
LintId::of(&copies::IF_SAME_THEN_ELSE),
1488+
LintId::of(&copies::SHARED_CODE_IN_IF_BLOCKS),
14881489
LintId::of(&default::FIELD_REASSIGN_WITH_DEFAULT),
14891490
LintId::of(&derive::DERIVE_HASH_XOR_EQ),
14901491
LintId::of(&derive::DERIVE_ORD_XOR_PARTIAL_ORD),
@@ -2063,7 +2064,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
20632064
store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
20642065
LintId::of(&attrs::EMPTY_LINE_AFTER_OUTER_ATTR),
20652066
LintId::of(&cognitive_complexity::COGNITIVE_COMPLEXITY),
2066-
LintId::of(&copies::SHARED_CODE_IN_IF_BLOCKS),
20672067
LintId::of(&disallowed_method::DISALLOWED_METHOD),
20682068
LintId::of(&fallible_impl_from::FALLIBLE_IMPL_FROM),
20692069
LintId::of(&floating_point_arithmetic::IMPRECISE_FLOPS),

clippy_utils/src/hir_utils.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,11 @@ impl HirEqInterExpr<'_, '_, '_> {
9696
pub fn eq_stmt(&mut self, left: &Stmt<'_>, right: &Stmt<'_>) -> bool {
9797
match (&left.kind, &right.kind) {
9898
(&StmtKind::Local(ref l), &StmtKind::Local(ref r)) => {
99-
self.eq_pat(&l.pat, &r.pat)
99+
// eq_pat adds the HirIds to the locals map. We therefor call it last to make sure that
100+
// these only get added if the init and type is equal.
101+
both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
100102
&& both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r))
101-
&& both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
103+
&& self.eq_pat(&l.pat, &r.pat)
102104
},
103105
(&StmtKind::Expr(ref l), &StmtKind::Expr(ref r)) | (&StmtKind::Semi(ref l), &StmtKind::Semi(ref r)) => {
104106
self.eq_expr(l, r)

clippy_utils/src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,9 +609,9 @@ pub fn get_pat_name(pat: &Pat<'_>) -> Option<Symbol> {
609609
}
610610
}
611611

612-
struct ContainsName {
613-
name: Symbol,
614-
result: bool,
612+
pub struct ContainsName {
613+
pub name: Symbol,
614+
pub result: bool,
615615
}
616616

617617
impl<'tcx> Visitor<'tcx> for ContainsName {
@@ -1216,6 +1216,8 @@ pub fn if_sequence<'tcx>(mut expr: &'tcx Expr<'tcx>) -> (Vec<&'tcx Expr<'tcx>>,
12161216
(conds, blocks)
12171217
}
12181218

1219+
/// This function returns true if the given expression is the `else` or `if else` part of an if
1220+
/// statement
12191221
pub fn parent_node_is_if_expr(expr: &Expr<'_>, cx: &LateContext<'_>) -> bool {
12201222
let map = cx.tcx.hir();
12211223
let parent_id = map.get_parent_node(expr.hir_id);
@@ -1326,6 +1328,16 @@ pub fn fn_def_id(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<DefId> {
13261328
}
13271329
}
13281330

1331+
/// This function checks if any of the lints in the slice is enabled for the provided `HirId`.
1332+
/// A lint counts as enabled with any of the levels: `Level::Forbid` | `Level::Deny` | `Level::Warn`
1333+
///
1334+
/// ```ignore
1335+
/// #[deny(clippy::YOUR_AWESOME_LINT)]
1336+
/// println!("Hello, World!"); // <- Clippy code: run_lints(cx, &[YOUR_AWESOME_LINT], id) == true
1337+
///
1338+
/// #[allow(clippy::YOUR_AWESOME_LINT)]
1339+
/// println!("See you soon!"); // <- Clippy code: run_lints(cx, &[YOUR_AWESOME_LINT], id) == false
1340+
/// ```
13291341
pub fn run_lints(cx: &LateContext<'_>, lints: &[&'static Lint], id: HirId) -> bool {
13301342
lints.iter().any(|lint| {
13311343
matches!(

tests/ui/checked_unwrap/complex_conditionals_nested.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
2-
#![allow(clippy::if_same_then_else)]
2+
#![allow(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)]
33

44
fn test_nested() {
55
fn nested() {

tests/ui/checked_unwrap/simple_conditionals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
2-
#![allow(clippy::if_same_then_else)]
2+
#![allow(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)]
33

44
macro_rules! m {
55
($a:expr) => {

tests/ui/default_numeric_fallback.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(clippy::never_loop)]
44
#![allow(clippy::no_effect)]
55
#![allow(clippy::unnecessary_operation)]
6+
#![allow(clippy::shared_code_in_if_blocks)]
67

78
mod basic_expr {
89
fn test() {

tests/ui/default_numeric_fallback.stderr

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,145 @@
11
error: default numeric fallback might occur
2-
--> $DIR/default_numeric_fallback.rs:10:17
2+
--> $DIR/default_numeric_fallback.rs:11:17
33
|
44
LL | let x = 22;
55
| ^^ help: consider adding suffix: `22_i32`
66
|
77
= note: `-D clippy::default-numeric-fallback` implied by `-D warnings`
88

99
error: default numeric fallback might occur
10-
--> $DIR/default_numeric_fallback.rs:11:18
10+
--> $DIR/default_numeric_fallback.rs:12:18
1111
|
1212
LL | let x = [1, 2, 3];
1313
| ^ help: consider adding suffix: `1_i32`
1414

1515
error: default numeric fallback might occur
16-
--> $DIR/default_numeric_fallback.rs:11:21
16+
--> $DIR/default_numeric_fallback.rs:12:21
1717
|
1818
LL | let x = [1, 2, 3];
1919
| ^ help: consider adding suffix: `2_i32`
2020

2121
error: default numeric fallback might occur
22-
--> $DIR/default_numeric_fallback.rs:11:24
22+
--> $DIR/default_numeric_fallback.rs:12:24
2323
|
2424
LL | let x = [1, 2, 3];
2525
| ^ help: consider adding suffix: `3_i32`
2626

2727
error: default numeric fallback might occur
28-
--> $DIR/default_numeric_fallback.rs:12:28
28+
--> $DIR/default_numeric_fallback.rs:13:28
2929
|
3030
LL | let x = if true { (1, 2) } else { (3, 4) };
3131
| ^ help: consider adding suffix: `1_i32`
3232

3333
error: default numeric fallback might occur
34-
--> $DIR/default_numeric_fallback.rs:12:31
34+
--> $DIR/default_numeric_fallback.rs:13:31
3535
|
3636
LL | let x = if true { (1, 2) } else { (3, 4) };
3737
| ^ help: consider adding suffix: `2_i32`
3838

3939
error: default numeric fallback might occur
40-
--> $DIR/default_numeric_fallback.rs:12:44
40+
--> $DIR/default_numeric_fallback.rs:13:44
4141
|
4242
LL | let x = if true { (1, 2) } else { (3, 4) };
4343
| ^ help: consider adding suffix: `3_i32`
4444

4545
error: default numeric fallback might occur
46-
--> $DIR/default_numeric_fallback.rs:12:47
46+
--> $DIR/default_numeric_fallback.rs:13:47
4747
|
4848
LL | let x = if true { (1, 2) } else { (3, 4) };
4949
| ^ help: consider adding suffix: `4_i32`
5050

5151
error: default numeric fallback might occur
52-
--> $DIR/default_numeric_fallback.rs:13:23
52+
--> $DIR/default_numeric_fallback.rs:14:23
5353
|
5454
LL | let x = match 1 {
5555
| ^ help: consider adding suffix: `1_i32`
5656

5757
error: default numeric fallback might occur
58-
--> $DIR/default_numeric_fallback.rs:14:13
58+
--> $DIR/default_numeric_fallback.rs:15:13
5959
|
6060
LL | 1 => 1,
6161
| ^ help: consider adding suffix: `1_i32`
6262

6363
error: default numeric fallback might occur
64-
--> $DIR/default_numeric_fallback.rs:14:18
64+
--> $DIR/default_numeric_fallback.rs:15:18
6565
|
6666
LL | 1 => 1,
6767
| ^ help: consider adding suffix: `1_i32`
6868

6969
error: default numeric fallback might occur
70-
--> $DIR/default_numeric_fallback.rs:15:18
70+
--> $DIR/default_numeric_fallback.rs:16:18
7171
|
7272
LL | _ => 2,
7373
| ^ help: consider adding suffix: `2_i32`
7474

7575
error: default numeric fallback might occur
76-
--> $DIR/default_numeric_fallback.rs:19:17
76+
--> $DIR/default_numeric_fallback.rs:20:17
7777
|
7878
LL | let x = 0.12;
7979
| ^^^^ help: consider adding suffix: `0.12_f64`
8080

8181
error: default numeric fallback might occur
82-
--> $DIR/default_numeric_fallback.rs:37:21
82+
--> $DIR/default_numeric_fallback.rs:38:21
8383
|
8484
LL | let y = 1;
8585
| ^ help: consider adding suffix: `1_i32`
8686

8787
error: default numeric fallback might occur
88-
--> $DIR/default_numeric_fallback.rs:45:21
88+
--> $DIR/default_numeric_fallback.rs:46:21
8989
|
9090
LL | let y = 1;
9191
| ^ help: consider adding suffix: `1_i32`
9292

9393
error: default numeric fallback might occur
94-
--> $DIR/default_numeric_fallback.rs:51:21
94+
--> $DIR/default_numeric_fallback.rs:52:21
9595
|
9696
LL | let y = 1;
9797
| ^ help: consider adding suffix: `1_i32`
9898

9999
error: default numeric fallback might occur
100-
--> $DIR/default_numeric_fallback.rs:63:9
100+
--> $DIR/default_numeric_fallback.rs:64:9
101101
|
102102
LL | 1
103103
| ^ help: consider adding suffix: `1_i32`
104104

105105
error: default numeric fallback might occur
106-
--> $DIR/default_numeric_fallback.rs:69:27
106+
--> $DIR/default_numeric_fallback.rs:70:27
107107
|
108108
LL | let f = || -> _ { 1 };
109109
| ^ help: consider adding suffix: `1_i32`
110110

111111
error: default numeric fallback might occur
112-
--> $DIR/default_numeric_fallback.rs:73:29
112+
--> $DIR/default_numeric_fallback.rs:74:29
113113
|
114114
LL | let f = || -> i32 { 1 };
115115
| ^ help: consider adding suffix: `1_i32`
116116

117117
error: default numeric fallback might occur
118-
--> $DIR/default_numeric_fallback.rs:87:21
118+
--> $DIR/default_numeric_fallback.rs:88:21
119119
|
120120
LL | generic_arg(1);
121121
| ^ help: consider adding suffix: `1_i32`
122122

123123
error: default numeric fallback might occur
124-
--> $DIR/default_numeric_fallback.rs:90:32
124+
--> $DIR/default_numeric_fallback.rs:91:32
125125
|
126126
LL | let x: _ = generic_arg(1);
127127
| ^ help: consider adding suffix: `1_i32`
128128

129129
error: default numeric fallback might occur
130-
--> $DIR/default_numeric_fallback.rs:108:28
130+
--> $DIR/default_numeric_fallback.rs:109:28
131131
|
132132
LL | GenericStruct { x: 1 };
133133
| ^ help: consider adding suffix: `1_i32`
134134

135135
error: default numeric fallback might occur
136-
--> $DIR/default_numeric_fallback.rs:111:36
136+
--> $DIR/default_numeric_fallback.rs:112:36
137137
|
138138
LL | let _ = GenericStruct { x: 1 };
139139
| ^ help: consider adding suffix: `1_i32`
140140

141141
error: default numeric fallback might occur
142-
--> $DIR/default_numeric_fallback.rs:131:23
142+
--> $DIR/default_numeric_fallback.rs:132:23
143143
|
144144
LL | s.generic_arg(1);
145145
| ^ help: consider adding suffix: `1_i32`

tests/ui/if_same_then_else.stderr

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,31 @@ LL | | 42
8282
LL | | };
8383
| |_____^
8484

85-
error: aborting due to 4 previous errors
85+
error: this `if` has identical blocks
86+
--> $DIR/if_same_then_else.rs:95:13
87+
|
88+
LL | if true {
89+
| _____________^
90+
LL | | let bar = if true { 42 } else { 43 };
91+
LL | |
92+
LL | | while foo() {
93+
... |
94+
LL | | bar + 1;
95+
LL | | } else {
96+
| |_____^
97+
|
98+
note: same as this
99+
--> $DIR/if_same_then_else.rs:102:12
100+
|
101+
LL | } else {
102+
| ____________^
103+
LL | | //~ ERROR same body as `if` block
104+
LL | | let bar = if true { 42 } else { 43 };
105+
LL | |
106+
... |
107+
LL | | bar + 1;
108+
LL | | }
109+
| |_____^
110+
111+
error: aborting due to 5 previous errors
86112

tests/ui/if_same_then_else2.stderr

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,25 @@ LL | | Ok("foo")?;
101101
LL | | }
102102
| |_____^
103103

104-
error: aborting due to 5 previous errors
104+
error: this `if` has identical blocks
105+
--> $DIR/if_same_then_else2.rs:122:20
106+
|
107+
LL | } else if true {
108+
| ____________________^
109+
LL | | let foo = "";
110+
LL | | return Ok(&foo[0..]);
111+
LL | | } else {
112+
| |_____^
113+
|
114+
note: same as this
115+
--> $DIR/if_same_then_else2.rs:125:12
116+
|
117+
LL | } else {
118+
| ____________^
119+
LL | | let foo = "";
120+
LL | | return Ok(&foo[0..]);
121+
LL | | }
122+
| |_____^
123+
124+
error: aborting due to 6 previous errors
105125

tests/ui/needless_bool/simple.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
dead_code,
55
clippy::no_effect,
66
clippy::if_same_then_else,
7-
clippy::needless_return
7+
clippy::needless_return,
8+
clippy::shared_code_in_if_blocks
89
)]
910

1011
fn main() {

tests/ui/needless_bool/simple.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this if-then-else expression will always return true
2-
--> $DIR/simple.rs:13:5
2+
--> $DIR/simple.rs:14:5
33
|
44
LL | / if x {
55
LL | | true
@@ -11,7 +11,7 @@ LL | | };
1111
= note: `-D clippy::needless-bool` implied by `-D warnings`
1212

1313
error: this if-then-else expression will always return false
14-
--> $DIR/simple.rs:18:5
14+
--> $DIR/simple.rs:19:5
1515
|
1616
LL | / if x {
1717
LL | | false
@@ -21,7 +21,7 @@ LL | | };
2121
| |_____^
2222

2323
error: this if-then-else expression will always return true
24-
--> $DIR/simple.rs:33:5
24+
--> $DIR/simple.rs:34:5
2525
|
2626
LL | / if x {
2727
LL | | return true;
@@ -31,7 +31,7 @@ LL | | };
3131
| |_____^
3232

3333
error: this if-then-else expression will always return false
34-
--> $DIR/simple.rs:41:5
34+
--> $DIR/simple.rs:42:5
3535
|
3636
LL | / if x {
3737
LL | | return false;

0 commit comments

Comments
 (0)