Skip to content

Commit 9d381da

Browse files
committed
Auto merge of #5752 - chrisduerr:pedantic_ranges, r=flip1995
Move range_minus_one to pedantic This moves the range_minus_one lint to the pedantic category, so there will not be any warnings emitted by default. This should work around problems where the suggestion is impossible to resolve due to the range consumer only accepting a specific range implementation, rather than the `RangeBounds` trait (see #3307). While it is possible to work around this by extracting the boundary into a variable, I don't think clippy should encourage people to disable or work around lints, but instead the lints should be fixable. So hopefully this will help until a proper implementation checks what the range is used for. *Please keep the line below* changelog: move [`range_minus_one`] to pedantic
2 parents 7d611d9 + afa4148 commit 9d381da

File tree

6 files changed

+23
-14
lines changed

6 files changed

+23
-14
lines changed

clippy_lints/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11611161
LintId::of(&needless_pass_by_value::NEEDLESS_PASS_BY_VALUE),
11621162
LintId::of(&non_expressive_names::SIMILAR_NAMES),
11631163
LintId::of(&option_if_let_else::OPTION_IF_LET_ELSE),
1164+
LintId::of(&ranges::RANGE_MINUS_ONE),
11641165
LintId::of(&ranges::RANGE_PLUS_ONE),
11651166
LintId::of(&shadow::SHADOW_UNRELATED),
11661167
LintId::of(&strings::STRING_ADD_ASSIGN),
@@ -1383,7 +1384,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13831384
LintId::of(&ptr::PTR_ARG),
13841385
LintId::of(&ptr_offset_with_cast::PTR_OFFSET_WITH_CAST),
13851386
LintId::of(&question_mark::QUESTION_MARK),
1386-
LintId::of(&ranges::RANGE_MINUS_ONE),
13871387
LintId::of(&ranges::RANGE_ZIP_WITH_LEN),
13881388
LintId::of(&ranges::REVERSED_EMPTY_RANGES),
13891389
LintId::of(&redundant_clone::REDUNDANT_CLONE),
@@ -1599,7 +1599,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
15991599
LintId::of(&partialeq_ne_impl::PARTIALEQ_NE_IMPL),
16001600
LintId::of(&precedence::PRECEDENCE),
16011601
LintId::of(&ptr_offset_with_cast::PTR_OFFSET_WITH_CAST),
1602-
LintId::of(&ranges::RANGE_MINUS_ONE),
16031602
LintId::of(&ranges::RANGE_ZIP_WITH_LEN),
16041603
LintId::of(&reference::DEREF_ADDROF),
16051604
LintId::of(&reference::REF_IN_DEREF),

clippy_lints/src/ranges.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ declare_clippy_lint! {
5252
/// exclusive ranges, because they essentially add an extra branch that
5353
/// LLVM may fail to hoist out of the loop.
5454
///
55+
/// This will cause a warning that cannot be fixed if the consumer of the
56+
/// range only accepts a specific range type, instead of the generic
57+
/// `RangeBounds` trait
58+
/// ([#3307](https://github.com/rust-lang/rust-clippy/issues/3307)).
59+
///
5560
/// **Example:**
5661
/// ```rust,ignore
5762
/// for x..(y+1) { .. }
@@ -72,7 +77,10 @@ declare_clippy_lint! {
7277
/// **Why is this bad?** The code is more readable with an exclusive range
7378
/// like `x..y`.
7479
///
75-
/// **Known problems:** None.
80+
/// **Known problems:** This will cause a warning that cannot be fixed if
81+
/// the consumer of the range only accepts a specific range type, instead of
82+
/// the generic `RangeBounds` trait
83+
/// ([#3307](https://github.com/rust-lang/rust-clippy/issues/3307)).
7684
///
7785
/// **Example:**
7886
/// ```rust,ignore
@@ -83,7 +91,7 @@ declare_clippy_lint! {
8391
/// for x..y { .. }
8492
/// ```
8593
pub RANGE_MINUS_ONE,
86-
complexity,
94+
pedantic,
8795
"`x..=(y-1)` reads better as `x..y`"
8896
}
8997

src/lintlist/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1790,7 +1790,7 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
17901790
},
17911791
Lint {
17921792
name: "range_minus_one",
1793-
group: "complexity",
1793+
group: "pedantic",
17941794
desc: "`x..=(y-1)` reads better as `x..y`",
17951795
deprecation: None,
17961796
module: "ranges",

tests/ui/range_plus_minus_one.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ fn f() -> usize {
77
}
88

99
#[warn(clippy::range_plus_one)]
10+
#[warn(clippy::range_minus_one)]
1011
fn main() {
1112
for _ in 0..2 {}
1213
for _ in 0..=2 {}

tests/ui/range_plus_minus_one.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ fn f() -> usize {
77
}
88

99
#[warn(clippy::range_plus_one)]
10+
#[warn(clippy::range_minus_one)]
1011
fn main() {
1112
for _ in 0..2 {}
1213
for _ in 0..=2 {}

tests/ui/range_plus_minus_one.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
11
error: an inclusive range would be more readable
2-
--> $DIR/range_plus_minus_one.rs:14:14
2+
--> $DIR/range_plus_minus_one.rs:15:14
33
|
44
LL | for _ in 0..3 + 1 {}
55
| ^^^^^^^^ help: use: `0..=3`
66
|
77
= note: `-D clippy::range-plus-one` implied by `-D warnings`
88

99
error: an inclusive range would be more readable
10-
--> $DIR/range_plus_minus_one.rs:17:14
10+
--> $DIR/range_plus_minus_one.rs:18:14
1111
|
1212
LL | for _ in 0..1 + 5 {}
1313
| ^^^^^^^^ help: use: `0..=5`
1414

1515
error: an inclusive range would be more readable
16-
--> $DIR/range_plus_minus_one.rs:20:14
16+
--> $DIR/range_plus_minus_one.rs:21:14
1717
|
1818
LL | for _ in 1..1 + 1 {}
1919
| ^^^^^^^^ help: use: `1..=1`
2020

2121
error: an inclusive range would be more readable
22-
--> $DIR/range_plus_minus_one.rs:26:14
22+
--> $DIR/range_plus_minus_one.rs:27:14
2323
|
2424
LL | for _ in 0..(1 + f()) {}
2525
| ^^^^^^^^^^^^ help: use: `0..=f()`
2626

2727
error: an exclusive range would be more readable
28-
--> $DIR/range_plus_minus_one.rs:30:13
28+
--> $DIR/range_plus_minus_one.rs:31:13
2929
|
3030
LL | let _ = ..=11 - 1;
3131
| ^^^^^^^^^ help: use: `..11`
3232
|
3333
= note: `-D clippy::range-minus-one` implied by `-D warnings`
3434

3535
error: an exclusive range would be more readable
36-
--> $DIR/range_plus_minus_one.rs:31:13
36+
--> $DIR/range_plus_minus_one.rs:32:13
3737
|
3838
LL | let _ = ..=(11 - 1);
3939
| ^^^^^^^^^^^ help: use: `..11`
4040

4141
error: an inclusive range would be more readable
42-
--> $DIR/range_plus_minus_one.rs:32:13
42+
--> $DIR/range_plus_minus_one.rs:33:13
4343
|
4444
LL | let _ = (1..11 + 1);
4545
| ^^^^^^^^^^^ help: use: `(1..=11)`
4646

4747
error: an inclusive range would be more readable
48-
--> $DIR/range_plus_minus_one.rs:33:13
48+
--> $DIR/range_plus_minus_one.rs:34:13
4949
|
5050
LL | let _ = (f() + 1)..(f() + 1);
5151
| ^^^^^^^^^^^^^^^^^^^^ help: use: `((f() + 1)..=f())`
5252

5353
error: an inclusive range would be more readable
54-
--> $DIR/range_plus_minus_one.rs:37:14
54+
--> $DIR/range_plus_minus_one.rs:38:14
5555
|
5656
LL | for _ in 1..ONE + ONE {}
5757
| ^^^^^^^^^^^^ help: use: `1..=ONE`

0 commit comments

Comments
 (0)