Skip to content

Commit 7ee35b1

Browse files
committed
Introduce LitKind::suffix
1 parent 8c0b4f6 commit 7ee35b1

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

compiler/rustc_ast/src/ast.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -1868,28 +1868,24 @@ impl LitKind {
18681868
matches!(self, LitKind::Int(..) | LitKind::Float(..))
18691869
}
18701870

1871-
/// Returns `true` if this literal has no suffix.
1872-
/// Note: this will return true for literals with prefixes such as raw strings and byte strings.
1873-
pub fn is_unsuffixed(&self) -> bool {
1874-
!self.is_suffixed()
1875-
}
1876-
1877-
/// Returns `true` if this literal has a suffix.
1878-
pub fn is_suffixed(&self) -> bool {
1871+
pub fn suffix(&self) -> Option<Symbol> {
18791872
match *self {
1880-
// suffixed variants
1881-
LitKind::Int(_, LitIntType::Signed(..) | LitIntType::Unsigned(..))
1882-
| LitKind::Float(_, LitFloatType::Suffixed(..)) => true,
1883-
// unsuffixed variants
1873+
LitKind::Int(_, kind) => match kind {
1874+
LitIntType::Signed(ty) => Some(ty.name()),
1875+
LitIntType::Unsigned(ty) => Some(ty.name()),
1876+
LitIntType::Unsuffixed => None,
1877+
}
1878+
LitKind::Float(_, kind) => match kind {
1879+
LitFloatType::Suffixed(ty) => Some(ty.name()),
1880+
LitFloatType::Unsuffixed => None,
1881+
}
18841882
LitKind::Str(..)
18851883
| LitKind::ByteStr(..)
18861884
| LitKind::CStr(..)
18871885
| LitKind::Byte(..)
18881886
| LitKind::Char(..)
1889-
| LitKind::Int(_, LitIntType::Unsuffixed)
1890-
| LitKind::Float(_, LitFloatType::Unsuffixed)
18911887
| LitKind::Bool(..)
1892-
| LitKind::Err => false,
1888+
| LitKind::Err => None,
18931889
}
18941890
}
18951891
}

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2806,7 +2806,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28062806
},
28072807
));
28082808
let literal_is_ty_suffixed = |expr: &hir::Expr<'_>| {
2809-
if let hir::ExprKind::Lit(lit) = &expr.kind { lit.node.is_suffixed() } else { false }
2809+
if let hir::ExprKind::Lit(lit) = &expr.kind { lit.node.suffix().is_some() } else { false }
28102810
};
28112811
let is_negative_int =
28122812
|expr: &hir::Expr<'_>| matches!(expr.kind, hir::ExprKind::Unary(hir::UnOp::Neg, ..));

compiler/rustc_parse/src/parser/attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ impl<'a> Parser<'a> {
326326
let lit = self.parse_meta_item_lit()?;
327327
debug!("checking if {:?} is unsuffixed", lit);
328328

329-
if !lit.kind.is_unsuffixed() {
329+
if lit.kind.suffix().is_some() {
330330
self.dcx().emit_err(SuffixedLiteralInAttribute { span: lit.span });
331331
}
332332

src/tools/clippy/clippy_lints/src/redundant_type_annotations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
197197
},
198198
LitKind::Int(..) | LitKind::Float(..) => {
199199
// If the initialization value is a suffixed literal we lint
200-
if init_lit.node.is_suffixed() {
200+
if init_lit.node.suffix().is_some() {
201201
span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "redundant type annotation");
202202
}
203203
},

0 commit comments

Comments
 (0)