Skip to content

Commit ff49e59

Browse files
committed
Introduce LitKind::suffix
1 parent 037f515 commit ff49e59

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

compiler/rustc_ast/src/ast.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -1872,28 +1872,24 @@ impl LitKind {
18721872
matches!(self, LitKind::Int(..) | LitKind::Float(..))
18731873
}
18741874

1875-
/// Returns `true` if this literal has no suffix.
1876-
/// Note: this will return true for literals with prefixes such as raw strings and byte strings.
1877-
pub fn is_unsuffixed(&self) -> bool {
1878-
!self.is_suffixed()
1879-
}
1880-
1881-
/// Returns `true` if this literal has a suffix.
1882-
pub fn is_suffixed(&self) -> bool {
1875+
pub fn suffix(&self) -> Option<Symbol> {
18831876
match *self {
1884-
// suffixed variants
1885-
LitKind::Int(_, LitIntType::Signed(..) | LitIntType::Unsigned(..))
1886-
| LitKind::Float(_, LitFloatType::Suffixed(..)) => true,
1887-
// unsuffixed variants
1877+
LitKind::Int(_, kind) => match kind {
1878+
LitIntType::Signed(ty) => Some(ty.name()),
1879+
LitIntType::Unsigned(ty) => Some(ty.name()),
1880+
LitIntType::Unsuffixed => None,
1881+
},
1882+
LitKind::Float(_, kind) => match kind {
1883+
LitFloatType::Suffixed(ty) => Some(ty.name()),
1884+
LitFloatType::Unsuffixed => None,
1885+
},
18881886
LitKind::Str(..)
18891887
| LitKind::ByteStr(..)
18901888
| LitKind::CStr(..)
18911889
| LitKind::Byte(..)
18921890
| LitKind::Char(..)
1893-
| LitKind::Int(_, LitIntType::Unsuffixed)
1894-
| LitKind::Float(_, LitFloatType::Unsuffixed)
18951891
| LitKind::Bool(..)
1896-
| LitKind::Err => false,
1892+
| LitKind::Err => None,
18971893
}
18981894
}
18991895
}

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2806,7 +2806,11 @@ 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 {
2810+
lit.node.suffix().is_some()
2811+
} else {
2812+
false
2813+
}
28102814
};
28112815
let is_negative_int =
28122816
|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)