Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 6db6a4f

Browse files
committed
Replace AssocOp::DotDot{,Eq} with AssocOp::Range.
It makes `AssocOp` more similar to `ExprKind` and makes things a little simpler. And the semantic names make more sense here than the syntactic names.
1 parent c79a7ed commit 6db6a4f

File tree

1 file changed

+8
-22
lines changed

1 file changed

+8
-22
lines changed

clippy_utils/src/sugg.rs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,7 @@ impl<'a> Sugg<'a> {
113113
/// function variants of `Sugg`, since these use different snippet functions.
114114
fn hir_from_snippet(expr: &hir::Expr<'_>, mut get_snippet: impl FnMut(Span) -> Cow<'a, str>) -> Self {
115115
if let Some(range) = higher::Range::hir(expr) {
116-
let op = match range.limits {
117-
ast::RangeLimits::HalfOpen => AssocOp::DotDot,
118-
ast::RangeLimits::Closed => AssocOp::DotDotEq,
119-
};
116+
let op = AssocOp::Range(range.limits);
120117
let start = range.start.map_or("".into(), |expr| get_snippet(expr.span));
121118
let end = range.end.map_or("".into(), |expr| get_snippet(expr.span));
122119

@@ -178,8 +175,6 @@ impl<'a> Sugg<'a> {
178175
ctxt: SyntaxContext,
179176
app: &mut Applicability,
180177
) -> Self {
181-
use rustc_ast::ast::RangeLimits;
182-
183178
let mut snippet = |span: Span| snippet_with_context(cx, span, ctxt, default, app).0;
184179

185180
match expr.kind {
@@ -228,13 +223,8 @@ impl<'a> Sugg<'a> {
228223
| ast::ExprKind::Err(_)
229224
| ast::ExprKind::Dummy
230225
| ast::ExprKind::UnsafeBinderCast(..) => Sugg::NonParen(snippet(expr.span)),
231-
ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::HalfOpen) => Sugg::BinOp(
232-
AssocOp::DotDot,
233-
lhs.as_ref().map_or("".into(), |lhs| snippet(lhs.span)),
234-
rhs.as_ref().map_or("".into(), |rhs| snippet(rhs.span)),
235-
),
236-
ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::Closed) => Sugg::BinOp(
237-
AssocOp::DotDotEq,
226+
ast::ExprKind::Range(ref lhs, ref rhs, limits) => Sugg::BinOp(
227+
AssocOp::Range(limits),
238228
lhs.as_ref().map_or("".into(), |lhs| snippet(lhs.span)),
239229
rhs.as_ref().map_or("".into(), |rhs| snippet(rhs.span)),
240230
),
@@ -326,11 +316,8 @@ impl<'a> Sugg<'a> {
326316

327317
/// Convenience method to create the `<lhs>..<rhs>` or `<lhs>...<rhs>`
328318
/// suggestion.
329-
pub fn range(self, end: &Self, limit: ast::RangeLimits) -> Sugg<'static> {
330-
match limit {
331-
ast::RangeLimits::HalfOpen => make_assoc(AssocOp::DotDot, &self, end),
332-
ast::RangeLimits::Closed => make_assoc(AssocOp::DotDotEq, &self, end),
333-
}
319+
pub fn range(self, end: &Self, limits: ast::RangeLimits) -> Sugg<'static> {
320+
make_assoc(AssocOp::Range(limits), &self, end)
334321
}
335322

336323
/// Adds parentheses to any expression that might need them. Suitable to the
@@ -370,8 +357,7 @@ fn binop_to_string(op: AssocOp, lhs: &str, rhs: &str) -> String {
370357
AssocOp::Assign => format!("{lhs} = {rhs}"),
371358
AssocOp::AssignOp(op) => format!("{lhs} {}= {rhs}", op.as_str()),
372359
AssocOp::As => format!("{lhs} as {rhs}"),
373-
AssocOp::DotDot => format!("{lhs}..{rhs}"),
374-
AssocOp::DotDotEq => format!("{lhs}..={rhs}"),
360+
AssocOp::Range(limits) => format!("{lhs}{}{rhs}", limits.as_str()),
375361
}
376362
}
377363

@@ -590,7 +576,7 @@ enum Associativity {
590576
/// associative.
591577
#[must_use]
592578
fn associativity(op: AssocOp) -> Associativity {
593-
use rustc_ast::util::parser::AssocOp::{As, Assign, AssignOp, Binary, DotDot, DotDotEq};
579+
use rustc_ast::util::parser::AssocOp::{As, Assign, AssignOp, Binary, Range};
594580
use ast::BinOpKind::{
595581
Add, BitAnd, BitOr, BitXor, Div, Eq, Gt, Ge, And, Or, Lt, Le, Rem, Mul, Ne, Shl, Shr, Sub,
596582
};
@@ -599,7 +585,7 @@ fn associativity(op: AssocOp) -> Associativity {
599585
Assign | AssignOp(_) => Associativity::Right,
600586
Binary(Add | BitAnd | BitOr | BitXor | And | Or | Mul) | As => Associativity::Both,
601587
Binary(Div | Eq | Gt | Ge | Lt | Le | Rem | Ne | Shl | Shr | Sub) => Associativity::Left,
602-
DotDot | DotDotEq => Associativity::None,
588+
Range(_) => Associativity::None,
603589
}
604590
}
605591

0 commit comments

Comments
 (0)