Skip to content

Commit

Permalink
Fix suggestion for ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
HMPerson1 committed Oct 18, 2019
1 parent 4578e5e commit a9cb2b9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions clippy_lints/src/utils/sugg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<'a> Sugg<'a> {
pub fn hir_opt(cx: &LateContext<'_, '_>, expr: &hir::Expr) -> Option<Self> {
snippet_opt(cx, expr.span).map(|snippet| {
let snippet = Cow::Owned(snippet);
Self::hir_from_snippet(expr, snippet)
Self::hir_from_snippet(cx, expr, snippet)
})
}

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

Self::hir_from_snippet(expr, snippet)
Self::hir_from_snippet(cx, expr, snippet)
}

/// Generate a suggestion for an expression with the given snippet. This is used by the `hir_*`
/// function variants of `Sugg`, since these use different snippet functions.
fn hir_from_snippet(expr: &hir::Expr, snippet: Cow<'a, str>) -> Self {
fn hir_from_snippet(cx: &LateContext<'_, '_>, expr: &hir::Expr, snippet: Cow<'a, str>) -> Self {
if let Some(range) = higher::range(cx, expr) {
let op = match range.limits {
ast::RangeLimits::HalfOpen => AssocOp::DotDot,
ast::RangeLimits::Closed => AssocOp::DotDotEq,
};
return Sugg::BinOp(op, snippet);
}

match expr.kind {
hir::ExprKind::AddrOf(..)
| hir::ExprKind::Box(..)
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/explicit_counter_loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ error: the variable `count` is used as a loop counter.
--> $DIR/explicit_counter_loop.rs:130:9
|
LL | for _i in 3..10 {
| ^^^^^^^^^^^^^^^ help: consider using: `for (count, _i) in 3..10.enumerate()`
| ^^^^^^^^^^^^^^^ help: consider using: `for (count, _i) in (3..10).enumerate()`

error: aborting due to 7 previous errors

0 comments on commit a9cb2b9

Please sign in to comment.