Skip to content

Commit a9cb2b9

Browse files
committed
Fix suggestion for ranges
1 parent 4578e5e commit a9cb2b9

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

clippy_lints/src/utils/sugg.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'a> Sugg<'a> {
4646
pub fn hir_opt(cx: &LateContext<'_, '_>, expr: &hir::Expr) -> Option<Self> {
4747
snippet_opt(cx, expr.span).map(|snippet| {
4848
let snippet = Cow::Owned(snippet);
49-
Self::hir_from_snippet(expr, snippet)
49+
Self::hir_from_snippet(cx, expr, snippet)
5050
})
5151
}
5252

@@ -84,12 +84,20 @@ impl<'a> Sugg<'a> {
8484
pub fn hir_with_macro_callsite(cx: &LateContext<'_, '_>, expr: &hir::Expr, default: &'a str) -> Self {
8585
let snippet = snippet_with_macro_callsite(cx, expr.span, default);
8686

87-
Self::hir_from_snippet(expr, snippet)
87+
Self::hir_from_snippet(cx, expr, snippet)
8888
}
8989

9090
/// Generate a suggestion for an expression with the given snippet. This is used by the `hir_*`
9191
/// function variants of `Sugg`, since these use different snippet functions.
92-
fn hir_from_snippet(expr: &hir::Expr, snippet: Cow<'a, str>) -> Self {
92+
fn hir_from_snippet(cx: &LateContext<'_, '_>, expr: &hir::Expr, snippet: Cow<'a, str>) -> Self {
93+
if let Some(range) = higher::range(cx, expr) {
94+
let op = match range.limits {
95+
ast::RangeLimits::HalfOpen => AssocOp::DotDot,
96+
ast::RangeLimits::Closed => AssocOp::DotDotEq,
97+
};
98+
return Sugg::BinOp(op, snippet);
99+
}
100+
93101
match expr.kind {
94102
hir::ExprKind::AddrOf(..)
95103
| hir::ExprKind::Box(..)

tests/ui/explicit_counter_loop.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ error: the variable `count` is used as a loop counter.
4040
--> $DIR/explicit_counter_loop.rs:130:9
4141
|
4242
LL | for _i in 3..10 {
43-
| ^^^^^^^^^^^^^^^ help: consider using: `for (count, _i) in 3..10.enumerate()`
43+
| ^^^^^^^^^^^^^^^ help: consider using: `for (count, _i) in (3..10).enumerate()`
4444

4545
error: aborting due to 7 previous errors
4646

0 commit comments

Comments
 (0)