about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_ast/src/ast.rs9
-rw-r--r--compiler/rustc_ast/src/util/parser.rs21
-rw-r--r--compiler/rustc_parse/src/parser/expr.rs15
-rw-r--r--src/tools/clippy/clippy_utils/src/sugg.rs30
4 files changed, 32 insertions, 43 deletions
diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs
index 65dd0a34aec..5c44fda2262 100644
--- a/compiler/rustc_ast/src/ast.rs
+++ b/compiler/rustc_ast/src/ast.rs
@@ -1453,6 +1453,15 @@ pub enum RangeLimits {
     Closed,
 }
 
+impl RangeLimits {
+    pub fn as_str(&self) -> &'static str {
+        match self {
+            RangeLimits::HalfOpen => "..",
+            RangeLimits::Closed => "..=",
+        }
+    }
+}
+
 /// A method call (e.g. `x.foo::<Bar, Baz>(a, b, c)`).
 #[derive(Clone, Encodable, Decodable, Debug)]
 pub struct MethodCall {
diff --git a/compiler/rustc_ast/src/util/parser.rs b/compiler/rustc_ast/src/util/parser.rs
index e7c26aa3092..c610f933074 100644
--- a/compiler/rustc_ast/src/util/parser.rs
+++ b/compiler/rustc_ast/src/util/parser.rs
@@ -1,6 +1,6 @@
 use rustc_span::kw;
 
-use crate::ast::{self, BinOpKind};
+use crate::ast::{self, BinOpKind, RangeLimits};
 use crate::token::{self, BinOpToken, Token};
 
 /// Associative operator.
@@ -14,10 +14,8 @@ pub enum AssocOp {
     Assign,
     /// `as`
     As,
-    /// `..` range
-    DotDot,
-    /// `..=` range
-    DotDotEq,
+    /// `..` or `..=` range
+    Range(RangeLimits),
 }
 
 #[derive(PartialEq, Debug)]
@@ -64,10 +62,9 @@ impl AssocOp {
             token::Ne => Some(Binary(BinOpKind::Ne)),
             token::AndAnd => Some(Binary(BinOpKind::And)),
             token::OrOr => Some(Binary(BinOpKind::Or)),
-            token::DotDot => Some(DotDot),
-            token::DotDotEq => Some(DotDotEq),
+            token::DotDot => Some(Range(RangeLimits::HalfOpen)),
             // DotDotDot is no longer supported, but we need some way to display the error
-            token::DotDotDot => Some(DotDotEq),
+            token::DotDotEq | token::DotDotDot => Some(Range(RangeLimits::Closed)),
             // `<-` should probably be `< -`
             token::LArrow => Some(Binary(BinOpKind::Lt)),
             _ if t.is_keyword(kw::As) => Some(As),
@@ -81,7 +78,7 @@ impl AssocOp {
         match *self {
             As => ExprPrecedence::Cast,
             Binary(bin_op) => bin_op.precedence(),
-            DotDot | DotDotEq => ExprPrecedence::Range,
+            Range(_) => ExprPrecedence::Range,
             Assign | AssignOp(_) => ExprPrecedence::Assign,
         }
     }
@@ -94,7 +91,7 @@ impl AssocOp {
             Assign | AssignOp(_) => Fixity::Right,
             Binary(binop) => binop.fixity(),
             As => Fixity::Left,
-            DotDot | DotDotEq => Fixity::None,
+            Range(_) => Fixity::None,
         }
     }
 
@@ -102,7 +99,7 @@ impl AssocOp {
         use AssocOp::*;
         match *self {
             Binary(binop) => binop.is_comparison(),
-            Assign | AssignOp(_) | As | DotDot | DotDotEq => false,
+            Assign | AssignOp(_) | As | Range(_) => false,
         }
     }
 
@@ -110,7 +107,7 @@ impl AssocOp {
         use AssocOp::*;
         match *self {
             Assign | AssignOp(_) => true,
-            As | Binary(_) | DotDot | DotDotEq => false,
+            As | Binary(_) | Range(_) => false,
         }
     }
 
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs
index 00a5f2fa28f..50ce6fa1a90 100644
--- a/compiler/rustc_parse/src/parser/expr.rs
+++ b/compiler/rustc_parse/src/parser/expr.rs
@@ -171,7 +171,7 @@ impl<'a> Parser<'a> {
                 break;
             }
             // Check for deprecated `...` syntax
-            if self.token == token::DotDotDot && op.node == AssocOp::DotDotEq {
+            if self.token == token::DotDotDot && op.node == AssocOp::Range(RangeLimits::Closed) {
                 self.err_dotdotdot_syntax(self.token.span);
             }
 
@@ -267,10 +267,10 @@ impl<'a> Parser<'a> {
             if op == AssocOp::As {
                 lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Cast)?;
                 continue;
-            } else if op == AssocOp::DotDot || op == AssocOp::DotDotEq {
+            } else if let AssocOp::Range(limits) = op {
                 // If we didn't have to handle `x..`/`x..=`, it would be pretty easy to
                 // generalise it to the Fixity::None code.
-                lhs = self.parse_expr_range(prec, lhs, op, cur_op_span)?;
+                lhs = self.parse_expr_range(prec, lhs, limits, cur_op_span)?;
                 break;
             }
 
@@ -294,7 +294,7 @@ impl<'a> Parser<'a> {
                     let aopexpr = self.mk_assign_op(source_map::respan(cur_op_span, aop), lhs, rhs);
                     self.mk_expr(span, aopexpr)
                 }
-                AssocOp::As | AssocOp::DotDot | AssocOp::DotDotEq => {
+                AssocOp::As | AssocOp::Range(_) => {
                     self.dcx().span_bug(span, "AssocOp should have been handled by special case")
                 }
             };
@@ -372,8 +372,7 @@ impl<'a> Parser<'a> {
                     AssocOp::Assign
                     | AssocOp::AssignOp(_)
                     | AssocOp::Binary(BinOpKind::BitOr)
-                    | AssocOp::DotDot
-                    | AssocOp::DotDotEq,
+                    | AssocOp::Range(_),
                 ),
                 _,
             ) if self.restrictions.contains(Restrictions::IS_PAT) => {
@@ -414,7 +413,7 @@ impl<'a> Parser<'a> {
         &mut self,
         prec: ExprPrecedence,
         lhs: P<Expr>,
-        op: AssocOp,
+        limits: RangeLimits,
         cur_op_span: Span,
     ) -> PResult<'a, P<Expr>> {
         let rhs = if self.is_at_start_of_range_notation_rhs() {
@@ -430,8 +429,6 @@ impl<'a> Parser<'a> {
         };
         let rhs_span = rhs.as_ref().map_or(cur_op_span, |x| x.span);
         let span = self.mk_expr_sp(&lhs, lhs.span, rhs_span);
-        let limits =
-            if op == AssocOp::DotDot { RangeLimits::HalfOpen } else { RangeLimits::Closed };
         let range = self.mk_range(Some(lhs), rhs, limits);
         Ok(self.mk_expr(span, range))
     }
diff --git a/src/tools/clippy/clippy_utils/src/sugg.rs b/src/tools/clippy/clippy_utils/src/sugg.rs
index 6e16778b343..fe5ae54fe50 100644
--- a/src/tools/clippy/clippy_utils/src/sugg.rs
+++ b/src/tools/clippy/clippy_utils/src/sugg.rs
@@ -113,10 +113,7 @@ impl<'a> Sugg<'a> {
     /// function variants of `Sugg`, since these use different snippet functions.
     fn hir_from_snippet(expr: &hir::Expr<'_>, mut get_snippet: impl FnMut(Span) -> Cow<'a, str>) -> Self {
         if let Some(range) = higher::Range::hir(expr) {
-            let op = match range.limits {
-                ast::RangeLimits::HalfOpen => AssocOp::DotDot,
-                ast::RangeLimits::Closed => AssocOp::DotDotEq,
-            };
+            let op = AssocOp::Range(range.limits);
             let start = range.start.map_or("".into(), |expr| get_snippet(expr.span));
             let end = range.end.map_or("".into(), |expr| get_snippet(expr.span));
 
@@ -178,8 +175,6 @@ impl<'a> Sugg<'a> {
         ctxt: SyntaxContext,
         app: &mut Applicability,
     ) -> Self {
-        use rustc_ast::ast::RangeLimits;
-
         let mut snippet = |span: Span| snippet_with_context(cx, span, ctxt, default, app).0;
 
         match expr.kind {
@@ -228,13 +223,8 @@ impl<'a> Sugg<'a> {
             | ast::ExprKind::Err(_)
             | ast::ExprKind::Dummy
             | ast::ExprKind::UnsafeBinderCast(..) => Sugg::NonParen(snippet(expr.span)),
-            ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::HalfOpen) => Sugg::BinOp(
-                AssocOp::DotDot,
-                lhs.as_ref().map_or("".into(), |lhs| snippet(lhs.span)),
-                rhs.as_ref().map_or("".into(), |rhs| snippet(rhs.span)),
-            ),
-            ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::Closed) => Sugg::BinOp(
-                AssocOp::DotDotEq,
+            ast::ExprKind::Range(ref lhs, ref rhs, limits) => Sugg::BinOp(
+                AssocOp::Range(limits),
                 lhs.as_ref().map_or("".into(), |lhs| snippet(lhs.span)),
                 rhs.as_ref().map_or("".into(), |rhs| snippet(rhs.span)),
             ),
@@ -326,11 +316,8 @@ impl<'a> Sugg<'a> {
 
     /// Convenience method to create the `<lhs>..<rhs>` or `<lhs>...<rhs>`
     /// suggestion.
-    pub fn range(self, end: &Self, limit: ast::RangeLimits) -> Sugg<'static> {
-        match limit {
-            ast::RangeLimits::HalfOpen => make_assoc(AssocOp::DotDot, &self, end),
-            ast::RangeLimits::Closed => make_assoc(AssocOp::DotDotEq, &self, end),
-        }
+    pub fn range(self, end: &Self, limits: ast::RangeLimits) -> Sugg<'static> {
+        make_assoc(AssocOp::Range(limits), &self, end)
     }
 
     /// 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 {
         AssocOp::Assign => format!("{lhs} = {rhs}"),
         AssocOp::AssignOp(op) => format!("{lhs} {}= {rhs}", op.as_str()),
         AssocOp::As => format!("{lhs} as {rhs}"),
-        AssocOp::DotDot => format!("{lhs}..{rhs}"),
-        AssocOp::DotDotEq => format!("{lhs}..={rhs}"),
+        AssocOp::Range(limits) => format!("{lhs}{}{rhs}", limits.as_str()),
     }
 }
 
@@ -590,7 +576,7 @@ enum Associativity {
 /// associative.
 #[must_use]
 fn associativity(op: AssocOp) -> Associativity {
-    use rustc_ast::util::parser::AssocOp::{As, Assign, AssignOp, Binary, DotDot, DotDotEq};
+    use rustc_ast::util::parser::AssocOp::{As, Assign, AssignOp, Binary, Range};
     use ast::BinOpKind::{
         Add, BitAnd, BitOr, BitXor, Div, Eq, Gt, Ge, And, Or, Lt, Le, Rem, Mul, Ne, Shl, Shr, Sub,
     };
@@ -599,7 +585,7 @@ fn associativity(op: AssocOp) -> Associativity {
         Assign | AssignOp(_) => Associativity::Right,
         Binary(Add | BitAnd | BitOr | BitXor | And | Or | Mul) | As => Associativity::Both,
         Binary(Div | Eq | Gt | Ge | Lt | Le | Rem | Ne | Shl | Shr | Sub) => Associativity::Left,
-        DotDot | DotDotEq => Associativity::None,
+        Range(_) => Associativity::None,
     }
 }