Skip to content

Commit ed38d9c

Browse files
authored
Merge pull request #3108 from daubaris/range-plus-one
range_plus_one suggestion should not remove braces fix
2 parents b7587d8 + 009c290 commit ed38d9c

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

clippy_lints/src/ranges.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use if_chain::if_chain;
44
use rustc::hir::*;
55
use syntax::ast::RangeLimits;
66
use syntax::source_map::Spanned;
7-
use crate::utils::{is_integer_literal, paths, snippet, span_lint, span_lint_and_then};
7+
use crate::utils::{is_integer_literal, paths, snippet, span_lint, span_lint_and_then, snippet_opt};
88
use crate::utils::{get_trait_def_id, higher, implements_trait, SpanlessEq};
99
use crate::utils::sugg::Sugg;
1010

@@ -49,7 +49,10 @@ declare_clippy_lint! {
4949
/// **Why is this bad?** The code is more readable with an inclusive range
5050
/// like `x..=y`.
5151
///
52-
/// **Known problems:** None.
52+
/// **Known problems:** Will add unnecessary pair of parentheses when the
53+
/// expression is not wrapped in a pair but starts with a opening parenthesis
54+
/// and ends with a closing one.
55+
/// I.e: `let _ = (f()+1)..(f()+1)` results in `let _ = ((f()+1)..=f())`.
5356
///
5457
/// **Example:**
5558
/// ```rust
@@ -145,9 +148,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
145148
|db| {
146149
let start = start.map_or("".to_owned(), |x| Sugg::hir(cx, x, "x").to_string());
147150
let end = Sugg::hir(cx, y, "y");
148-
db.span_suggestion(expr.span,
151+
if let Some(is_wrapped) = &snippet_opt(cx, expr.span) {
152+
if is_wrapped.starts_with('(') && is_wrapped.ends_with(')') {
153+
db.span_suggestion(expr.span,
154+
"use",
155+
format!("({}..={})", start, end));
156+
} else {
157+
db.span_suggestion(expr.span,
149158
"use",
150159
format!("{}..={}", start, end));
160+
}
161+
}
151162
},
152163
);
153164
}

tests/ui/range_plus_minus_one.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ fn main() {
2727
let _ = ..11-1;
2828
let _ = ..=11-1;
2929
let _ = ..=(11-1);
30+
let _ = (1..11+1);
3031
let _ = (f()+1)..(f()+1);
3132

3233
let mut vec: Vec<()> = std::vec::Vec::new();

tests/ui/range_plus_minus_one.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ error: an exclusive range would be more readable
4141
error: an inclusive range would be more readable
4242
--> $DIR/range_plus_minus_one.rs:30:13
4343
|
44-
30 | let _ = (f()+1)..(f()+1);
45-
| ^^^^^^^^^^^^^^^^ help: use: `(f()+1)..=f()`
44+
30 | let _ = (1..11+1);
45+
| ^^^^^^^^^ help: use: `(1..=11)`
4646

47-
error: aborting due to 7 previous errors
47+
error: an inclusive range would be more readable
48+
--> $DIR/range_plus_minus_one.rs:31:13
49+
|
50+
31 | let _ = (f()+1)..(f()+1);
51+
| ^^^^^^^^^^^^^^^^ help: use: `((f()+1)..=f())`
52+
53+
error: aborting due to 8 previous errors
4854

0 commit comments

Comments
 (0)