|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_then; |
2 | 2 | use clippy_utils::sugg; |
3 | 3 | use rustc_ast::ast::{BinOpKind, Expr, ExprKind, LitKind, UnOp}; |
| 4 | +use rustc_ast::util::parser::AssocOp; |
4 | 5 | use rustc_data_structures::packed::Pu128; |
5 | 6 | use rustc_errors::Applicability; |
6 | 7 | use rustc_lint::{EarlyContext, EarlyLintPass}; |
@@ -134,12 +135,19 @@ impl IntPlusOne { |
134 | 135 | |diag| { |
135 | 136 | let mut app = Applicability::MachineApplicable; |
136 | 137 | let ctxt = expr.span.ctxt(); |
137 | | - let new_lhs = sugg::Sugg::ast(cx, new_lhs, "_", ctxt, &mut app); |
| 138 | + let mut new_lhs = sugg::Sugg::ast(cx, new_lhs, "_", ctxt, &mut app); |
138 | 139 | let new_rhs = sugg::Sugg::ast(cx, new_rhs, "_", ctxt, &mut app); |
139 | 140 | let new_binop = match le_or_ge { |
140 | 141 | LeOrGe::Ge => BinOpKind::Gt, |
141 | 142 | LeOrGe::Le => BinOpKind::Lt, |
142 | 143 | }; |
| 144 | + // When the replacement operator is `<`, an `as` cast on the LHS |
| 145 | + // must be parenthesized. Otherwise, the parser interprets the `<` |
| 146 | + // as the start of generic arguments on the cast type |
| 147 | + // (e.g., `x as usize < y` is parsed as `x as usize<y>`). |
| 148 | + if matches!(new_lhs, sugg::Sugg::BinOp(AssocOp::Cast, ..)) && new_binop == BinOpKind::Lt { |
| 149 | + new_lhs = new_lhs.maybe_paren(); |
| 150 | + } |
143 | 151 | let rec = sugg::make_binop(new_binop, &new_lhs, &new_rhs); |
144 | 152 | diag.span_suggestion(expr.span, "change it to", rec, app); |
145 | 153 | }, |
|
0 commit comments