Skip to content

Commit 610535c

Browse files
committed
Renaming the lint to branches_sharing_code and fixing typos
1 parent 128b71b commit 610535c

22 files changed

+71
-69
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,7 @@ Released 2018-09-13
21292129
[`borrowed_box`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrowed_box
21302130
[`box_vec`]: https://rust-lang.github.io/rust-clippy/master/index.html#box_vec
21312131
[`boxed_local`]: https://rust-lang.github.io/rust-clippy/master/index.html#boxed_local
2132+
[`branches_sharing_code`]: https://rust-lang.github.io/rust-clippy/master/index.html#branches_sharing_code
21322133
[`builtin_type_shadow`]: https://rust-lang.github.io/rust-clippy/master/index.html#builtin_type_shadow
21332134
[`bytes_nth`]: https://rust-lang.github.io/rust-clippy/master/index.html#bytes_nth
21342135
[`cargo_common_metadata`]: https://rust-lang.github.io/rust-clippy/master/index.html#cargo_common_metadata
@@ -2455,7 +2456,6 @@ Released 2018-09-13
24552456
[`shadow_reuse`]: https://rust-lang.github.io/rust-clippy/master/index.html#shadow_reuse
24562457
[`shadow_same`]: https://rust-lang.github.io/rust-clippy/master/index.html#shadow_same
24572458
[`shadow_unrelated`]: https://rust-lang.github.io/rust-clippy/master/index.html#shadow_unrelated
2458-
[`shared_code_in_if_blocks`]: https://rust-lang.github.io/rust-clippy/master/index.html#shared_code_in_if_blocks
24592459
[`short_circuit_statement`]: https://rust-lang.github.io/rust-clippy/master/index.html#short_circuit_statement
24602460
[`should_assert_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#should_assert_eq
24612461
[`should_implement_trait`]: https://rust-lang.github.io/rust-clippy/master/index.html#should_implement_trait

clippy_lints/src/copies.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::utils::{
2-
both, count_eq, eq_expr_value, first_line_of_span, get_enclosing_block, get_parent_expr, if_sequence, in_macro,
3-
indent_of, parent_node_is_if_expr, reindent_multiline, run_lints, search_same, snippet, snippet_opt,
4-
span_lint_and_note, span_lint_and_then, ContainsName, SpanlessEq, SpanlessHash,
1+
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_then};
2+
use clippy_utils::source::{first_line_of_span, indent_of, reindent_multiline, snippet, snippet_opt};
3+
use clippy_utils::{
4+
both, count_eq, eq_expr_value, get_enclosing_block, get_parent_expr, if_sequence, in_macro, parent_node_is_if_expr,
5+
run_lints, search_same, ContainsName, SpanlessEq, SpanlessHash,
56
};
67
use if_chain::if_chain;
78
use rustc_data_structures::fx::FxHashSet;
@@ -141,7 +142,7 @@ declare_clippy_lint! {
141142
/// 42
142143
/// };
143144
/// ```
144-
pub SHARED_CODE_IN_IF_BLOCKS,
145+
pub BRANCHES_SHARING_CODE,
145146
complexity,
146147
"`if` statement with shared code in all blocks"
147148
}
@@ -150,7 +151,7 @@ declare_lint_pass!(CopyAndPaste => [
150151
IFS_SAME_COND,
151152
SAME_FUNCTIONS_IN_IF_CONDITION,
152153
IF_SAME_THEN_ELSE,
153-
SHARED_CODE_IN_IF_BLOCKS
154+
BRANCHES_SHARING_CODE
154155
]);
155156

156157
impl<'tcx> LateLintPass<'tcx> for CopyAndPaste {
@@ -173,17 +174,17 @@ impl<'tcx> LateLintPass<'tcx> for CopyAndPaste {
173174
lint_same_cond(cx, &conds);
174175
lint_same_fns_in_if_cond(cx, &conds);
175176
// Block duplication
176-
lint_same_then_else(cx, &blocks, conds.len() != blocks.len(), expr);
177+
lint_same_then_else(cx, &blocks, conds.len() == blocks.len(), expr);
177178
}
178179
}
179180
}
180181
}
181182

182-
/// Implementation of `SHARED_CODE_IN_IF_BLOCKS` and `IF_SAME_THEN_ELSE` if the blocks are equal.
183+
/// Implementation of `BRANCHES_SHARING_CODE` and `IF_SAME_THEN_ELSE` if the blocks are equal.
183184
fn lint_same_then_else<'tcx>(
184185
cx: &LateContext<'tcx>,
185186
blocks: &[&Block<'tcx>],
186-
has_unconditional_else: bool,
187+
has_conditional_else: bool,
187188
expr: &'tcx Expr<'_>,
188189
) {
189190
// We only lint ifs with multiple blocks
@@ -195,8 +196,8 @@ fn lint_same_then_else<'tcx>(
195196
let has_expr = blocks[0].expr.is_some();
196197
let (start_eq, mut end_eq, expr_eq) = scan_block_for_eq(cx, blocks);
197198

198-
// SHARED_CODE_IN_IF_BLOCKS prerequisites
199-
if !has_unconditional_else || (start_eq == 0 && end_eq == 0 && (has_expr && !expr_eq)) {
199+
// BRANCHES_SHARING_CODE prerequisites
200+
if has_conditional_else || (start_eq == 0 && end_eq == 0 && (has_expr && !expr_eq)) {
200201
return;
201202
}
202203

@@ -210,7 +211,7 @@ fn lint_same_then_else<'tcx>(
210211
intravisit::walk_stmt(&mut start_walker, stmt);
211212
}
212213

213-
emit_shared_code_in_if_blocks_lint(
214+
emit_branches_sharing_code_lint(
214215
cx,
215216
start_eq,
216217
0,
@@ -277,7 +278,7 @@ fn lint_same_then_else<'tcx>(
277278
});
278279
}
279280

280-
emit_shared_code_in_if_blocks_lint(
281+
emit_branches_sharing_code_lint(
281282
cx,
282283
start_eq,
283284
end_eq,
@@ -298,7 +299,7 @@ fn scan_block_for_eq(cx: &LateContext<'tcx>, blocks: &[&Block<'tcx>]) -> (usize,
298299
let r_stmts = win[1].stmts;
299300

300301
// `SpanlessEq` now keeps track of the locals and is therefore context sensitive clippy#6752.
301-
// The comparison therefor needs to be done in a way that builds the correct context.
302+
// The comparison therefore needs to be done in a way that builds the correct context.
302303
let mut evaluator = SpanlessEq::new(cx);
303304
let mut evaluator = evaluator.inter_expr();
304305

@@ -387,7 +388,7 @@ fn check_for_warn_of_moved_symbol(
387388
})
388389
}
389390

390-
fn emit_shared_code_in_if_blocks_lint(
391+
fn emit_branches_sharing_code_lint(
391392
cx: &LateContext<'tcx>,
392393
start_stmts: usize,
393394
end_stmts: usize,
@@ -472,7 +473,7 @@ fn emit_shared_code_in_if_blocks_lint(
472473
let (place_str, span, sugg) = suggestions.pop().unwrap();
473474
let msg = format!("all if blocks contain the same code at the {}", place_str);
474475
let help = format!("consider moving the {} statements out like this", place_str);
475-
span_lint_and_then(cx, SHARED_CODE_IN_IF_BLOCKS, span, msg.as_str(), |diag| {
476+
span_lint_and_then(cx, BRANCHES_SHARING_CODE, span, msg.as_str(), |diag| {
476477
diag.span_suggestion(span, help.as_str(), sugg, Applicability::Unspecified);
477478

478479
add_optional_msgs(diag);
@@ -482,7 +483,7 @@ fn emit_shared_code_in_if_blocks_lint(
482483
let (_, start_span, start_sugg) = suggestions.pop().unwrap();
483484
span_lint_and_then(
484485
cx,
485-
SHARED_CODE_IN_IF_BLOCKS,
486+
BRANCHES_SHARING_CODE,
486487
start_span,
487488
"all if blocks contain the same code at the start and the end. Here at the start",
488489
move |diag| {

clippy_lints/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -614,10 +614,10 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
614614
&collapsible_if::COLLAPSIBLE_IF,
615615
&collapsible_match::COLLAPSIBLE_MATCH,
616616
&comparison_chain::COMPARISON_CHAIN,
617+
&copies::BRANCHES_SHARING_CODE,
617618
&copies::IFS_SAME_COND,
618619
&copies::IF_SAME_THEN_ELSE,
619620
&copies::SAME_FUNCTIONS_IN_IF_CONDITION,
620-
&copies::SHARED_CODE_IN_IF_BLOCKS,
621621
&copy_iterator::COPY_ITERATOR,
622622
&create_dir::CREATE_DIR,
623623
&dbg_macro::DBG_MACRO,
@@ -1484,9 +1484,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14841484
LintId::of(&collapsible_if::COLLAPSIBLE_IF),
14851485
LintId::of(&collapsible_match::COLLAPSIBLE_MATCH),
14861486
LintId::of(&comparison_chain::COMPARISON_CHAIN),
1487+
LintId::of(&copies::BRANCHES_SHARING_CODE),
14871488
LintId::of(&copies::IFS_SAME_COND),
14881489
LintId::of(&copies::IF_SAME_THEN_ELSE),
1489-
LintId::of(&copies::SHARED_CODE_IN_IF_BLOCKS),
14901490
LintId::of(&default::FIELD_REASSIGN_WITH_DEFAULT),
14911491
LintId::of(&derive::DERIVE_HASH_XOR_EQ),
14921492
LintId::of(&derive::DERIVE_ORD_XOR_PARTIAL_ORD),
@@ -1873,6 +1873,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
18731873
LintId::of(&booleans::NONMINIMAL_BOOL),
18741874
LintId::of(&casts::CHAR_LIT_AS_U8),
18751875
LintId::of(&casts::UNNECESSARY_CAST),
1876+
LintId::of(&copies::BRANCHES_SHARING_CODE),
18761877
LintId::of(&double_comparison::DOUBLE_COMPARISONS),
18771878
LintId::of(&double_parens::DOUBLE_PARENS),
18781879
LintId::of(&duration_subsec::DURATION_SUBSEC),

clippy_utils/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub mod usage;
4747
pub mod visitors;
4848

4949
pub use self::attrs::*;
50-
pub use self::hir_utils::{both, eq_expr_value, over, SpanlessEq, SpanlessHash};
50+
pub use self::hir_utils::{both, count_eq, eq_expr_value, over, SpanlessEq, SpanlessHash};
5151

5252
use std::collections::hash_map::Entry;
5353
use std::hash::BuildHasherDefault;

tests/ui/shared_code_in_if_blocks/shared_at_bot.rs tests/ui/branches_sharing_code/shared_at_bottom.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(dead_code)]
2-
#![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)]
2+
#![deny(clippy::if_same_then_else, clippy::branches_sharing_code)]
33

4-
// This tests the shared_code_in_if_blocks lint at the end of blocks
4+
// This tests the branches_sharing_code lint at the end of blocks
55

66
fn simple_examples() {
77
let x = 1;

tests/ui/shared_code_in_if_blocks/shared_at_bot.stderr tests/ui/branches_sharing_code/shared_at_bottom.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: all if blocks contain the same code at the end
2-
--> $DIR/shared_at_bot.rs:30:5
2+
--> $DIR/shared_at_bottom.rs:30:5
33
|
44
LL | / let result = false;
55
LL | | println!("Block end!");
@@ -8,10 +8,10 @@ LL | | };
88
| |_____^
99
|
1010
note: the lint level is defined here
11-
--> $DIR/shared_at_bot.rs:2:36
11+
--> $DIR/shared_at_bottom.rs:2:36
1212
|
13-
LL | #![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)]
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
LL | #![deny(clippy::if_same_then_else, clippy::branches_sharing_code)]
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515
= note: The end suggestion probably needs some adjustments to use the expression result correctly
1616
help: consider moving the end statements out like this
1717
|
@@ -22,7 +22,7 @@ LL | result;
2222
|
2323

2424
error: all if blocks contain the same code at the end
25-
--> $DIR/shared_at_bot.rs:48:5
25+
--> $DIR/shared_at_bottom.rs:48:5
2626
|
2727
LL | / println!("Same end of block");
2828
LL | | }
@@ -35,7 +35,7 @@ LL | println!("Same end of block");
3535
|
3636

3737
error: all if blocks contain the same code at the end
38-
--> $DIR/shared_at_bot.rs:65:5
38+
--> $DIR/shared_at_bottom.rs:65:5
3939
|
4040
LL | / println!(
4141
LL | | "I'm moveable because I know: `outer_scope_value`: '{}'",
@@ -54,7 +54,7 @@ LL | );
5454
|
5555

5656
error: all if blocks contain the same code at the end
57-
--> $DIR/shared_at_bot.rs:77:9
57+
--> $DIR/shared_at_bottom.rs:77:9
5858
|
5959
LL | / println!("Hello World");
6060
LL | | }
@@ -67,7 +67,7 @@ LL | println!("Hello World");
6767
|
6868

6969
error: all if blocks contain the same code at the end
70-
--> $DIR/shared_at_bot.rs:93:5
70+
--> $DIR/shared_at_bottom.rs:93:5
7171
|
7272
LL | / let later_used_value = "A string value";
7373
LL | | println!("{}", later_used_value);
@@ -84,7 +84,7 @@ LL | println!("{}", later_used_value);
8484
|
8585

8686
error: all if blocks contain the same code at the end
87-
--> $DIR/shared_at_bot.rs:106:5
87+
--> $DIR/shared_at_bottom.rs:106:5
8888
|
8989
LL | / let simple_examples = "I now identify as a &str :)";
9090
LL | | println!("This is the new simple_example: {}", simple_examples);
@@ -100,7 +100,7 @@ LL | println!("This is the new simple_example: {}", simple_examples);
100100
|
101101

102102
error: all if blocks contain the same code at the end
103-
--> $DIR/shared_at_bot.rs:171:5
103+
--> $DIR/shared_at_bottom.rs:171:5
104104
|
105105
LL | / x << 2
106106
LL | | };
@@ -114,7 +114,7 @@ LL | x << 2;
114114
|
115115

116116
error: all if blocks contain the same code at the end
117-
--> $DIR/shared_at_bot.rs:178:5
117+
--> $DIR/shared_at_bottom.rs:178:5
118118
|
119119
LL | / x * 4
120120
LL | | }
@@ -128,7 +128,7 @@ LL | x * 4
128128
|
129129

130130
error: all if blocks contain the same code at the end
131-
--> $DIR/shared_at_bot.rs:190:44
131+
--> $DIR/shared_at_bottom.rs:190:44
132132
|
133133
LL | if x == 17 { b = 1; a = 0x99; } else { a = 0x99; }
134134
| ^^^^^^^^^^^

tests/ui/shared_code_in_if_blocks/shared_at_top.rs tests/ui/branches_sharing_code/shared_at_top.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(dead_code, clippy::eval_order_dependence)]
2-
#![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)]
2+
#![deny(clippy::if_same_then_else, clippy::branches_sharing_code)]
33

4-
// This tests the shared_code_in_if_blocks lint at the start of blocks
4+
// This tests the branches_sharing_code lint at the start of blocks
55

66
fn simple_examples() {
77
let x = 0;

tests/ui/shared_code_in_if_blocks/shared_at_top.stderr tests/ui/branches_sharing_code/shared_at_top.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | | println!("Hello World!");
88
note: the lint level is defined here
99
--> $DIR/shared_at_top.rs:2:36
1010
|
11-
LL | #![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)]
12-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
LL | #![deny(clippy::if_same_then_else, clippy::branches_sharing_code)]
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1313
help: consider moving the start statements out like this
1414
|
1515
LL | println!("Hello World!");
@@ -106,7 +106,7 @@ LL | | } else {
106106
note: the lint level is defined here
107107
--> $DIR/shared_at_top.rs:2:9
108108
|
109-
LL | #![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)]
109+
LL | #![deny(clippy::if_same_then_else, clippy::branches_sharing_code)]
110110
| ^^^^^^^^^^^^^^^^^^^^^^^^^
111111
note: same as this
112112
--> $DIR/shared_at_top.rs:98:12

tests/ui/shared_code_in_if_blocks/shared_at_top_and_bot.rs tests/ui/branches_sharing_code/shared_at_top_and_bottom.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(dead_code)]
2-
#![deny(clippy::if_same_then_else, clippy::shared_code_in_if_blocks)]
2+
#![deny(clippy::if_same_then_else, clippy::branches_sharing_code)]
33

4-
// shared_code_in_if_blocks at the top and bottom of the if blocks
4+
// branches_sharing_code at the top and bottom of the if blocks
55

66
struct DataPack {
77
id: u32,

0 commit comments

Comments
 (0)