@@ -4,7 +4,7 @@ use if_chain::if_chain;
4
4
use rustc:: hir:: * ;
5
5
use syntax:: ast:: RangeLimits ;
6
6
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 } ;
8
8
use crate :: utils:: { get_trait_def_id, higher, implements_trait, SpanlessEq } ;
9
9
use crate :: utils:: sugg:: Sugg ;
10
10
@@ -49,7 +49,10 @@ declare_clippy_lint! {
49
49
/// **Why is this bad?** The code is more readable with an inclusive range
50
50
/// like `x..=y`.
51
51
///
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())`.
53
56
///
54
57
/// **Example:**
55
58
/// ```rust
@@ -145,9 +148,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
145
148
|db| {
146
149
let start = start. map_or( "" . to_owned( ) , |x| Sugg :: hir( cx, x, "x" ) . to_string( ) ) ;
147
150
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,
149
158
"use" ,
150
159
format!( "{}..={}" , start, end) ) ;
160
+ }
161
+ }
151
162
} ,
152
163
) ;
153
164
}
0 commit comments