2017-07-19 23:56:32 +00:00
|
|
|
//! Lints concerned with the grouping of digits with underscores in integral or
|
|
|
|
//! floating-point literal expressions.
|
|
|
|
|
2019-05-15 12:57:56 +00:00
|
|
|
use crate::utils::{in_macro, snippet_opt, span_lint_and_sugg};
|
2018-10-23 21:54:27 +00:00
|
|
|
use if_chain::if_chain;
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc::lint::in_external_macro;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
|
2020-02-21 08:39:38 +00:00
|
|
|
use syntax::ast::{Expr, ExprKind, Lit, LitFloatType, LitIntType, LitKind};
|
2017-07-19 23:56:32 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Warns if a long integral or floating-point constant does
|
|
|
|
/// not contain underscores.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Reading long numbers is difficult without separators.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-05 22:23:50 +00:00
|
|
|
/// let x: u64 = 61864918973511;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2017-07-19 23:56:32 +00:00
|
|
|
pub UNREADABLE_LITERAL,
|
2018-03-28 13:24:26 +00:00
|
|
|
style,
|
2017-07-19 23:56:32 +00:00
|
|
|
"long integer literal without underscores"
|
|
|
|
}
|
|
|
|
|
2018-09-02 21:07:55 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Warns for mistyped suffix in literals
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** This is most probably a typo
|
|
|
|
///
|
|
|
|
/// **Known problems:**
|
2019-10-03 20:07:51 +00:00
|
|
|
/// - Recommends a signed suffix, even though the number might be too big and an unsigned
|
|
|
|
/// suffix is required
|
2019-11-26 00:30:46 +00:00
|
|
|
/// - Does not match on `_127` since that is a valid grouping for decimal and octal numbers
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-05 22:23:50 +00:00
|
|
|
/// 2_32;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2018-09-02 21:07:55 +00:00
|
|
|
pub MISTYPED_LITERAL_SUFFIXES,
|
|
|
|
correctness,
|
|
|
|
"mistyped literal suffix"
|
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Warns if an integral or floating-point constant is
|
|
|
|
/// grouped inconsistently with underscores.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Readers may incorrectly interpret inconsistently
|
|
|
|
/// grouped digits.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-05 22:23:50 +00:00
|
|
|
/// let x: u64 = 618_64_9189_73_511;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2017-07-19 23:56:32 +00:00
|
|
|
pub INCONSISTENT_DIGIT_GROUPING,
|
2018-03-28 13:24:26 +00:00
|
|
|
style,
|
2017-07-19 23:56:32 +00:00
|
|
|
"integer literals with digits grouped inconsistently"
|
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Warns if the digits of an integral or floating-point
|
|
|
|
/// constant are grouped into groups that
|
|
|
|
/// are too large.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Negatively impacts readability.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-05 22:23:50 +00:00
|
|
|
/// let x: u64 = 6186491_8973511;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2017-07-19 23:56:32 +00:00
|
|
|
pub LARGE_DIGIT_GROUPS,
|
2018-12-02 00:46:03 +00:00
|
|
|
pedantic,
|
2017-07-19 23:56:32 +00:00
|
|
|
"grouping digits into groups that are too large"
|
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Warns if there is a better representation for a numeric literal.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Especially for big powers of 2 a hexadecimal representation is more
|
|
|
|
/// readable than a decimal representation.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// `255` => `0xFF`
|
|
|
|
/// `65_535` => `0xFFFF`
|
|
|
|
/// `4_042_322_160` => `0xF0F0_F0F0`
|
2018-01-23 14:29:31 +00:00
|
|
|
pub DECIMAL_LITERAL_REPRESENTATION,
|
2018-03-28 13:24:26 +00:00
|
|
|
restriction,
|
2018-01-16 13:01:07 +00:00
|
|
|
"using decimal representation when hexadecimal would be better"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
2018-05-02 18:40:52 +00:00
|
|
|
pub(super) enum Radix {
|
2017-07-30 22:33:15 +00:00
|
|
|
Binary,
|
|
|
|
Octal,
|
|
|
|
Decimal,
|
|
|
|
Hexadecimal,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Radix {
|
2019-01-31 01:15:29 +00:00
|
|
|
/// Returns a reasonable digit group size for this radix.
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2019-11-13 06:28:50 +00:00
|
|
|
fn suggest_grouping(&self) -> usize {
|
2017-07-30 22:33:15 +00:00
|
|
|
match *self {
|
2019-07-31 00:25:35 +00:00
|
|
|
Self::Binary | Self::Hexadecimal => 4,
|
|
|
|
Self::Octal | Self::Decimal => 3,
|
2017-07-30 22:33:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 06:28:50 +00:00
|
|
|
/// A helper method to format numeric literals with digit grouping.
|
|
|
|
/// `lit` must be a valid numeric literal without suffix.
|
|
|
|
pub fn format_numeric_literal(lit: &str, type_suffix: Option<&str>, float: bool) -> String {
|
|
|
|
NumericLiteral::new(lit, type_suffix, float).format()
|
|
|
|
}
|
|
|
|
|
2017-07-30 22:33:15 +00:00
|
|
|
#[derive(Debug)]
|
2019-11-13 06:28:01 +00:00
|
|
|
pub(super) struct NumericLiteral<'a> {
|
2017-07-30 22:33:15 +00:00
|
|
|
/// Which radix the literal was represented in.
|
2019-11-13 06:28:50 +00:00
|
|
|
radix: Radix,
|
2017-07-30 22:33:15 +00:00
|
|
|
/// The radix prefix, if present.
|
2019-11-13 06:28:50 +00:00
|
|
|
prefix: Option<&'a str>,
|
2019-11-13 06:27:49 +00:00
|
|
|
|
|
|
|
/// The integer part of the number.
|
|
|
|
integer: &'a str,
|
|
|
|
/// The fraction part of the number.
|
|
|
|
fraction: Option<&'a str>,
|
|
|
|
/// The character used as exponent seperator (b'e' or b'E') and the exponent part.
|
|
|
|
exponent: Option<(char, &'a str)>,
|
|
|
|
|
2017-07-30 22:33:15 +00:00
|
|
|
/// The type suffix, including preceding underscore if present.
|
2019-11-13 06:28:50 +00:00
|
|
|
suffix: Option<&'a str>,
|
2017-07-30 22:33:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 06:28:01 +00:00
|
|
|
impl<'a> NumericLiteral<'a> {
|
|
|
|
fn from_lit(src: &'a str, lit: &Lit) -> Option<NumericLiteral<'a>> {
|
2019-11-13 06:27:54 +00:00
|
|
|
if lit.kind.is_numeric() && src.chars().next().map_or(false, |c| c.is_digit(10)) {
|
|
|
|
let (unsuffixed, suffix) = split_suffix(&src, &lit.kind);
|
|
|
|
let float = if let LitKind::Float(..) = lit.kind { true } else { false };
|
2019-11-13 06:28:01 +00:00
|
|
|
Some(NumericLiteral::new(unsuffixed, suffix, float))
|
2019-11-13 06:27:54 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2019-11-13 06:28:50 +00:00
|
|
|
fn new(lit: &'a str, suffix: Option<&'a str>, float: bool) -> Self {
|
2017-07-30 22:33:15 +00:00
|
|
|
// Determine delimiter for radix prefix, if present, and radix.
|
|
|
|
let radix = if lit.starts_with("0x") {
|
|
|
|
Radix::Hexadecimal
|
|
|
|
} else if lit.starts_with("0b") {
|
|
|
|
Radix::Binary
|
|
|
|
} else if lit.starts_with("0o") {
|
|
|
|
Radix::Octal
|
|
|
|
} else {
|
|
|
|
Radix::Decimal
|
|
|
|
};
|
|
|
|
|
|
|
|
// Grab part of the literal after prefix, if present.
|
2019-11-13 06:27:54 +00:00
|
|
|
let (prefix, mut sans_prefix) = if let Radix::Decimal = radix {
|
2017-07-30 22:33:15 +00:00
|
|
|
(None, lit)
|
|
|
|
} else {
|
|
|
|
let (p, s) = lit.split_at(2);
|
|
|
|
(Some(p), s)
|
|
|
|
};
|
|
|
|
|
2019-11-13 06:27:54 +00:00
|
|
|
if suffix.is_some() && sans_prefix.ends_with('_') {
|
|
|
|
// The '_' before the suffix isn't part of the digits
|
|
|
|
sans_prefix = &sans_prefix[..sans_prefix.len() - 1];
|
2017-07-30 22:33:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 06:27:54 +00:00
|
|
|
let (integer, fraction, exponent) = Self::split_digit_parts(sans_prefix, float);
|
2019-11-13 06:27:49 +00:00
|
|
|
|
2017-08-21 11:32:12 +00:00
|
|
|
Self {
|
2018-03-15 15:07:15 +00:00
|
|
|
radix,
|
|
|
|
prefix,
|
2019-11-13 06:27:49 +00:00
|
|
|
integer,
|
|
|
|
fraction,
|
|
|
|
exponent,
|
|
|
|
suffix,
|
2017-07-30 22:33:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-30 22:37:11 +00:00
|
|
|
|
2019-11-13 06:27:49 +00:00
|
|
|
fn split_digit_parts(digits: &str, float: bool) -> (&str, Option<&str>, Option<(char, &str)>) {
|
2019-11-13 06:27:19 +00:00
|
|
|
let mut integer = digits;
|
|
|
|
let mut fraction = None;
|
|
|
|
let mut exponent = None;
|
|
|
|
|
2019-11-13 06:27:49 +00:00
|
|
|
if float {
|
2019-11-13 06:27:19 +00:00
|
|
|
for (i, c) in digits.char_indices() {
|
|
|
|
match c {
|
|
|
|
'.' => {
|
|
|
|
integer = &digits[..i];
|
|
|
|
fraction = Some(&digits[i + 1..]);
|
|
|
|
},
|
|
|
|
'e' | 'E' => {
|
|
|
|
if integer.len() > i {
|
|
|
|
integer = &digits[..i];
|
|
|
|
} else {
|
|
|
|
fraction = Some(&digits[integer.len() + 1..i]);
|
|
|
|
};
|
|
|
|
exponent = Some((c, &digits[i + 1..]));
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(integer, fraction, exponent)
|
|
|
|
}
|
|
|
|
|
2018-09-02 21:07:55 +00:00
|
|
|
/// Returns literal formatted in a sensible way.
|
2019-11-13 06:28:50 +00:00
|
|
|
fn format(&self) -> String {
|
2019-11-13 06:27:19 +00:00
|
|
|
let mut output = String::new();
|
|
|
|
|
|
|
|
if let Some(prefix) = self.prefix {
|
|
|
|
output.push_str(prefix);
|
|
|
|
}
|
|
|
|
|
2017-07-30 22:37:11 +00:00
|
|
|
let group_size = self.radix.suggest_grouping();
|
2019-11-13 06:27:19 +00:00
|
|
|
|
2019-11-13 06:27:49 +00:00
|
|
|
Self::group_digits(
|
|
|
|
&mut output,
|
|
|
|
self.integer,
|
|
|
|
group_size,
|
|
|
|
true,
|
|
|
|
self.radix == Radix::Hexadecimal,
|
|
|
|
);
|
2019-11-13 06:27:19 +00:00
|
|
|
|
2019-11-13 06:27:49 +00:00
|
|
|
if let Some(fraction) = self.fraction {
|
2019-11-13 06:27:19 +00:00
|
|
|
output.push('.');
|
2019-11-13 06:27:37 +00:00
|
|
|
Self::group_digits(&mut output, fraction, group_size, false, false);
|
2019-11-13 06:27:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 06:27:49 +00:00
|
|
|
if let Some((separator, exponent)) = self.exponent {
|
|
|
|
output.push(separator);
|
2019-11-13 06:27:37 +00:00
|
|
|
Self::group_digits(&mut output, exponent, group_size, true, false);
|
2019-11-13 06:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(suffix) = self.suffix {
|
2019-11-13 06:27:54 +00:00
|
|
|
output.push('_');
|
|
|
|
output.push_str(suffix);
|
2017-07-30 22:37:11 +00:00
|
|
|
}
|
2019-11-13 06:27:19 +00:00
|
|
|
|
|
|
|
output
|
2017-07-30 22:37:11 +00:00
|
|
|
}
|
2019-11-13 06:27:37 +00:00
|
|
|
|
|
|
|
fn group_digits(output: &mut String, input: &str, group_size: usize, partial_group_first: bool, pad: bool) {
|
|
|
|
debug_assert!(group_size > 0);
|
|
|
|
|
|
|
|
let mut digits = input.chars().filter(|&c| c != '_');
|
|
|
|
|
|
|
|
let first_group_size;
|
|
|
|
|
|
|
|
if partial_group_first {
|
2019-11-14 05:42:04 +00:00
|
|
|
first_group_size = (digits.clone().count() - 1) % group_size + 1;
|
2019-11-13 06:27:37 +00:00
|
|
|
if pad {
|
|
|
|
for _ in 0..group_size - first_group_size {
|
|
|
|
output.push('0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
first_group_size = group_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
for _ in 0..first_group_size {
|
|
|
|
if let Some(digit) = digits.next() {
|
|
|
|
output.push(digit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (c, i) in digits.zip((0..group_size).cycle()) {
|
|
|
|
if i == 0 {
|
|
|
|
output.push('_');
|
|
|
|
}
|
|
|
|
output.push(c);
|
|
|
|
}
|
|
|
|
}
|
2017-07-30 22:33:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 06:27:54 +00:00
|
|
|
fn split_suffix<'a>(src: &'a str, lit_kind: &LitKind) -> (&'a str, Option<&'a str>) {
|
|
|
|
debug_assert!(lit_kind.is_numeric());
|
|
|
|
if let Some(suffix_length) = lit_suffix_length(lit_kind) {
|
|
|
|
let (unsuffixed, suffix) = src.split_at(src.len() - suffix_length);
|
|
|
|
(unsuffixed, Some(suffix))
|
|
|
|
} else {
|
|
|
|
(src, None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lit_suffix_length(lit_kind: &LitKind) -> Option<usize> {
|
|
|
|
debug_assert!(lit_kind.is_numeric());
|
|
|
|
let suffix = match lit_kind {
|
|
|
|
LitKind::Int(_, int_lit_kind) => match int_lit_kind {
|
|
|
|
LitIntType::Signed(int_ty) => Some(int_ty.name_str()),
|
|
|
|
LitIntType::Unsigned(uint_ty) => Some(uint_ty.name_str()),
|
|
|
|
LitIntType::Unsuffixed => None,
|
|
|
|
},
|
|
|
|
LitKind::Float(_, float_lit_kind) => match float_lit_kind {
|
|
|
|
LitFloatType::Suffixed(float_ty) => Some(float_ty.name_str()),
|
|
|
|
LitFloatType::Unsuffixed => None,
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
suffix.map(str::len)
|
|
|
|
}
|
|
|
|
|
2017-07-30 22:33:15 +00:00
|
|
|
enum WarningType {
|
|
|
|
UnreadableLiteral,
|
|
|
|
InconsistentDigitGrouping,
|
|
|
|
LargeDigitGroups,
|
2018-01-23 14:29:31 +00:00
|
|
|
DecimalRepresentation,
|
2018-10-23 21:54:27 +00:00
|
|
|
MistypedLiteralSuffix,
|
2017-07-30 22:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WarningType {
|
2019-12-31 00:17:56 +00:00
|
|
|
fn display(&self, suggested_format: String, cx: &EarlyContext<'_>, span: rustc_span::Span) {
|
2018-05-31 18:15:48 +00:00
|
|
|
match self {
|
2019-07-31 00:25:35 +00:00
|
|
|
Self::MistypedLiteralSuffix => span_lint_and_sugg(
|
2018-10-23 21:54:27 +00:00
|
|
|
cx,
|
|
|
|
MISTYPED_LITERAL_SUFFIXES,
|
|
|
|
span,
|
|
|
|
"mistyped literal suffix",
|
|
|
|
"did you mean to write",
|
2019-11-13 06:28:06 +00:00
|
|
|
suggested_format,
|
2018-11-27 15:59:39 +00:00
|
|
|
Applicability::MaybeIncorrect,
|
2018-10-23 21:54:27 +00:00
|
|
|
),
|
2019-07-31 00:25:35 +00:00
|
|
|
Self::UnreadableLiteral => span_lint_and_sugg(
|
2017-09-05 09:33:04 +00:00
|
|
|
cx,
|
|
|
|
UNREADABLE_LITERAL,
|
2018-05-31 18:15:48 +00:00
|
|
|
span,
|
2017-09-05 09:33:04 +00:00
|
|
|
"long literal lacking separators",
|
2018-03-01 15:15:41 +00:00
|
|
|
"consider",
|
2019-11-13 06:28:06 +00:00
|
|
|
suggested_format,
|
2018-11-27 14:13:57 +00:00
|
|
|
Applicability::MachineApplicable,
|
2017-09-05 09:33:04 +00:00
|
|
|
),
|
2019-07-31 00:25:35 +00:00
|
|
|
Self::LargeDigitGroups => span_lint_and_sugg(
|
2017-09-05 09:33:04 +00:00
|
|
|
cx,
|
|
|
|
LARGE_DIGIT_GROUPS,
|
2018-05-31 18:15:48 +00:00
|
|
|
span,
|
2017-09-05 09:33:04 +00:00
|
|
|
"digit groups should be smaller",
|
2018-03-01 15:15:41 +00:00
|
|
|
"consider",
|
2019-11-13 06:28:06 +00:00
|
|
|
suggested_format,
|
2018-11-27 14:13:57 +00:00
|
|
|
Applicability::MachineApplicable,
|
2017-09-05 09:33:04 +00:00
|
|
|
),
|
2019-07-31 00:25:35 +00:00
|
|
|
Self::InconsistentDigitGrouping => span_lint_and_sugg(
|
2017-09-05 09:33:04 +00:00
|
|
|
cx,
|
|
|
|
INCONSISTENT_DIGIT_GROUPING,
|
2018-05-31 18:15:48 +00:00
|
|
|
span,
|
2017-09-05 09:33:04 +00:00
|
|
|
"digits grouped inconsistently by underscores",
|
2018-03-01 15:15:41 +00:00
|
|
|
"consider",
|
2019-11-13 06:28:06 +00:00
|
|
|
suggested_format,
|
2018-11-27 14:13:57 +00:00
|
|
|
Applicability::MachineApplicable,
|
2017-09-05 09:33:04 +00:00
|
|
|
),
|
2019-07-31 00:25:35 +00:00
|
|
|
Self::DecimalRepresentation => span_lint_and_sugg(
|
2018-01-16 13:01:07 +00:00
|
|
|
cx,
|
2018-01-23 14:29:31 +00:00
|
|
|
DECIMAL_LITERAL_REPRESENTATION,
|
2018-05-31 18:15:48 +00:00
|
|
|
span,
|
2018-01-23 14:29:31 +00:00
|
|
|
"integer literal has a better hexadecimal representation",
|
2018-03-01 15:15:41 +00:00
|
|
|
"consider",
|
2019-11-13 06:28:06 +00:00
|
|
|
suggested_format,
|
2018-11-27 14:13:57 +00:00
|
|
|
Applicability::MachineApplicable,
|
2018-01-16 13:01:07 +00:00
|
|
|
),
|
2017-07-30 22:33:15 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(LiteralDigitGrouping => [
|
|
|
|
UNREADABLE_LITERAL,
|
|
|
|
INCONSISTENT_DIGIT_GROUPING,
|
|
|
|
LARGE_DIGIT_GROUPS,
|
|
|
|
MISTYPED_LITERAL_SUFFIXES,
|
|
|
|
]);
|
2017-07-19 23:56:32 +00:00
|
|
|
|
|
|
|
impl EarlyLintPass for LiteralDigitGrouping {
|
2018-07-23 11:01:12 +00:00
|
|
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
2018-07-24 06:55:38 +00:00
|
|
|
if in_external_macro(cx.sess(), expr.span) {
|
2017-07-19 23:56:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Lit(ref lit) = expr.kind {
|
2019-10-03 19:09:32 +00:00
|
|
|
Self::check_lit(cx, lit)
|
2017-07-19 23:56:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LiteralDigitGrouping {
|
2019-10-03 19:09:32 +00:00
|
|
|
fn check_lit(cx: &EarlyContext<'_>, lit: &Lit) {
|
2019-11-13 06:26:52 +00:00
|
|
|
if_chain! {
|
|
|
|
if let Some(src) = snippet_opt(cx, lit.span);
|
2019-11-13 06:28:01 +00:00
|
|
|
if let Some(mut num_lit) = NumericLiteral::from_lit(&src, &lit);
|
2019-11-13 06:26:52 +00:00
|
|
|
then {
|
2019-11-13 06:28:01 +00:00
|
|
|
if !Self::check_for_mistyped_suffix(cx, lit.span, &mut num_lit) {
|
2019-11-13 06:27:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-13 06:26:52 +00:00
|
|
|
|
2019-11-13 06:27:05 +00:00
|
|
|
let result = (|| {
|
2019-11-13 06:27:14 +00:00
|
|
|
|
2019-11-14 06:08:24 +00:00
|
|
|
let integral_group_size = Self::get_group_size(num_lit.integer.split('_'))?;
|
2019-11-13 06:28:01 +00:00
|
|
|
if let Some(fraction) = num_lit.fraction {
|
2019-11-14 06:08:24 +00:00
|
|
|
let fractional_group_size = Self::get_group_size(fraction.rsplit('_'))?;
|
2019-11-13 06:27:27 +00:00
|
|
|
|
|
|
|
let consistent = Self::parts_consistent(integral_group_size,
|
|
|
|
fractional_group_size,
|
2019-11-13 06:28:01 +00:00
|
|
|
num_lit.integer.len(),
|
2019-11-13 06:27:27 +00:00
|
|
|
fraction.len());
|
|
|
|
if !consistent {
|
|
|
|
return Err(WarningType::InconsistentDigitGrouping);
|
|
|
|
};
|
2019-11-13 06:27:05 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
if let Err(warning_type) = result {
|
2019-11-14 06:08:24 +00:00
|
|
|
let should_warn = match warning_type {
|
|
|
|
| WarningType::UnreadableLiteral
|
|
|
|
| WarningType::InconsistentDigitGrouping
|
|
|
|
| WarningType::LargeDigitGroups => {
|
|
|
|
!in_macro(lit.span)
|
|
|
|
}
|
|
|
|
WarningType::DecimalRepresentation | WarningType::MistypedLiteralSuffix => {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if should_warn {
|
|
|
|
warning_type.display(num_lit.format(), cx, lit.span)
|
|
|
|
}
|
2019-11-13 06:27:05 +00:00
|
|
|
}
|
2019-11-13 06:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-19 23:56:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 06:27:54 +00:00
|
|
|
// Returns `false` if the check fails
|
|
|
|
fn check_for_mistyped_suffix(
|
|
|
|
cx: &EarlyContext<'_>,
|
2019-12-31 00:17:56 +00:00
|
|
|
span: rustc_span::Span,
|
2019-11-13 06:28:01 +00:00
|
|
|
num_lit: &mut NumericLiteral<'_>,
|
2019-11-13 06:27:54 +00:00
|
|
|
) -> bool {
|
2019-11-13 06:28:01 +00:00
|
|
|
if num_lit.suffix.is_some() {
|
2019-11-13 06:27:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-13 06:28:01 +00:00
|
|
|
let (part, mistyped_suffixes, missing_char) = if let Some((_, exponent)) = &mut num_lit.exponent {
|
2019-11-13 06:27:54 +00:00
|
|
|
(exponent, &["32", "64"][..], 'f')
|
2019-11-13 06:28:01 +00:00
|
|
|
} else if let Some(fraction) = &mut num_lit.fraction {
|
2019-11-13 06:27:54 +00:00
|
|
|
(fraction, &["32", "64"][..], 'f')
|
|
|
|
} else {
|
2019-11-13 06:28:01 +00:00
|
|
|
(&mut num_lit.integer, &["8", "16", "32", "64"][..], 'i')
|
2019-11-13 06:27:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut split = part.rsplit('_');
|
|
|
|
let last_group = split.next().expect("At least one group");
|
|
|
|
if split.next().is_some() && mistyped_suffixes.contains(&last_group) {
|
|
|
|
*part = &part[..part.len() - last_group.len()];
|
2019-11-13 06:28:06 +00:00
|
|
|
let mut sugg = num_lit.format();
|
|
|
|
sugg.push('_');
|
|
|
|
sugg.push(missing_char);
|
|
|
|
sugg.push_str(last_group);
|
|
|
|
WarningType::MistypedLiteralSuffix.display(sugg, cx, span);
|
2019-11-13 06:27:54 +00:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-30 22:54:56 +00:00
|
|
|
/// Given the sizes of the digit groups of both integral and fractional
|
|
|
|
/// parts, and the length
|
|
|
|
/// of both parts, determine if the digits have been grouped consistently.
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2019-11-13 06:27:42 +00:00
|
|
|
fn parts_consistent(
|
|
|
|
int_group_size: Option<usize>,
|
|
|
|
frac_group_size: Option<usize>,
|
|
|
|
int_size: usize,
|
|
|
|
frac_size: usize,
|
|
|
|
) -> bool {
|
2017-07-30 22:54:56 +00:00
|
|
|
match (int_group_size, frac_group_size) {
|
|
|
|
// No groups on either side of decimal point - trivially consistent.
|
2019-11-13 06:27:42 +00:00
|
|
|
(None, None) => true,
|
2017-07-30 22:54:56 +00:00
|
|
|
// Integral part has grouped digits, fractional part does not.
|
2019-11-13 06:27:42 +00:00
|
|
|
(Some(int_group_size), None) => frac_size <= int_group_size,
|
2017-07-30 22:54:56 +00:00
|
|
|
// Fractional part has grouped digits, integral part does not.
|
2019-11-13 06:27:42 +00:00
|
|
|
(None, Some(frac_group_size)) => int_size <= frac_group_size,
|
2017-07-30 22:54:56 +00:00
|
|
|
// Both parts have grouped digits. Groups should be the same size.
|
2019-11-13 06:27:42 +00:00
|
|
|
(Some(int_group_size), Some(frac_group_size)) => int_group_size == frac_group_size,
|
2017-07-19 23:56:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 06:27:42 +00:00
|
|
|
/// Returns the size of the digit groups (or None if ungrouped) if successful,
|
|
|
|
/// otherwise returns a `WarningType` for linting.
|
2019-11-14 06:08:24 +00:00
|
|
|
fn get_group_size<'a>(groups: impl Iterator<Item = &'a str>) -> Result<Option<usize>, WarningType> {
|
2019-11-13 06:27:42 +00:00
|
|
|
let mut groups = groups.map(str::len);
|
|
|
|
|
|
|
|
let first = groups.next().expect("At least one group");
|
|
|
|
|
|
|
|
if let Some(second) = groups.next() {
|
|
|
|
if !groups.all(|x| x == second) || first > second {
|
|
|
|
Err(WarningType::InconsistentDigitGrouping)
|
|
|
|
} else if second > 4 {
|
|
|
|
Err(WarningType::LargeDigitGroups)
|
2017-07-19 23:56:32 +00:00
|
|
|
} else {
|
2019-11-13 06:27:42 +00:00
|
|
|
Ok(Some(second))
|
2017-07-19 23:56:32 +00:00
|
|
|
}
|
2019-11-14 06:08:24 +00:00
|
|
|
} else if first > 5 {
|
2019-11-13 06:27:42 +00:00
|
|
|
Err(WarningType::UnreadableLiteral)
|
2017-07-19 23:56:32 +00:00
|
|
|
} else {
|
2019-11-13 06:27:42 +00:00
|
|
|
Ok(None)
|
2017-07-19 23:56:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 13:01:07 +00:00
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
#[allow(clippy::module_name_repetitions)]
|
2018-01-16 13:01:07 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2019-04-08 20:43:55 +00:00
|
|
|
pub struct DecimalLiteralRepresentation {
|
2018-01-23 11:34:40 +00:00
|
|
|
threshold: u64,
|
|
|
|
}
|
2018-01-16 13:01:07 +00:00
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
impl_lint_pass!(DecimalLiteralRepresentation => [DECIMAL_LITERAL_REPRESENTATION]);
|
2018-01-16 13:01:07 +00:00
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
impl EarlyLintPass for DecimalLiteralRepresentation {
|
2018-07-23 11:01:12 +00:00
|
|
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
2018-07-24 06:55:38 +00:00
|
|
|
if in_external_macro(cx.sess(), expr.span) {
|
2018-01-16 13:01:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Lit(ref lit) = expr.kind {
|
2018-01-16 13:01:07 +00:00
|
|
|
self.check_lit(cx, lit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
impl DecimalLiteralRepresentation {
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2018-01-23 11:34:40 +00:00
|
|
|
pub fn new(threshold: u64) -> Self {
|
2018-10-23 21:54:27 +00:00
|
|
|
Self { threshold }
|
2018-01-23 11:34:40 +00:00
|
|
|
}
|
2018-07-23 11:01:12 +00:00
|
|
|
fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
|
2018-01-16 13:01:07 +00:00
|
|
|
// Lint integral literals.
|
|
|
|
if_chain! {
|
2019-10-31 07:13:08 +00:00
|
|
|
if let LitKind::Int(val, _) = lit.kind;
|
2018-01-16 13:01:07 +00:00
|
|
|
if let Some(src) = snippet_opt(cx, lit.span);
|
2019-11-13 06:28:01 +00:00
|
|
|
if let Some(num_lit) = NumericLiteral::from_lit(&src, &lit);
|
|
|
|
if num_lit.radix == Radix::Decimal;
|
2019-04-10 19:05:56 +00:00
|
|
|
if val >= u128::from(self.threshold);
|
2018-01-16 13:01:07 +00:00
|
|
|
then {
|
2019-04-10 19:05:56 +00:00
|
|
|
let hex = format!("{:#X}", val);
|
2019-12-25 16:22:36 +00:00
|
|
|
let num_lit = NumericLiteral::new(&hex, num_lit.suffix, false);
|
2019-11-13 06:28:01 +00:00
|
|
|
let _ = Self::do_lint(num_lit.integer).map_err(|warning_type| {
|
2019-11-13 06:28:06 +00:00
|
|
|
warning_type.display(num_lit.format(), cx, lit.span)
|
2019-04-10 19:05:56 +00:00
|
|
|
});
|
2018-01-16 13:01:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_lint(digits: &str) -> Result<(), WarningType> {
|
2018-01-23 11:34:40 +00:00
|
|
|
if digits.len() == 1 {
|
|
|
|
// Lint for 1 digit literals, if someone really sets the threshold that low
|
2018-10-23 21:54:27 +00:00
|
|
|
if digits == "1"
|
|
|
|
|| digits == "2"
|
|
|
|
|| digits == "4"
|
|
|
|
|| digits == "8"
|
|
|
|
|| digits == "3"
|
|
|
|
|| digits == "7"
|
2018-01-23 11:34:40 +00:00
|
|
|
|| digits == "F"
|
|
|
|
{
|
2018-01-23 14:29:31 +00:00
|
|
|
return Err(WarningType::DecimalRepresentation);
|
2018-01-23 11:34:40 +00:00
|
|
|
}
|
|
|
|
} else if digits.len() < 4 {
|
|
|
|
// Lint for Literals with a hex-representation of 2 or 3 digits
|
2018-01-16 13:01:07 +00:00
|
|
|
let f = &digits[0..1]; // first digit
|
|
|
|
let s = &digits[1..]; // suffix
|
2018-11-01 21:43:40 +00:00
|
|
|
|
2018-01-23 11:34:40 +00:00
|
|
|
// Powers of 2
|
|
|
|
if ((f.eq("1") || f.eq("2") || f.eq("4") || f.eq("8")) && s.chars().all(|c| c == '0'))
|
|
|
|
// Powers of 2 minus 1
|
|
|
|
|| ((f.eq("1") || f.eq("3") || f.eq("7") || f.eq("F")) && s.chars().all(|c| c == 'F'))
|
|
|
|
{
|
2018-01-23 14:29:31 +00:00
|
|
|
return Err(WarningType::DecimalRepresentation);
|
2018-01-16 13:01:07 +00:00
|
|
|
}
|
2018-01-23 11:34:40 +00:00
|
|
|
} else {
|
2018-01-16 13:01:07 +00:00
|
|
|
// Lint for Literals with a hex-representation of 4 digits or more
|
|
|
|
let f = &digits[0..1]; // first digit
|
|
|
|
let m = &digits[1..digits.len() - 1]; // middle digits, except last
|
|
|
|
let s = &digits[1..]; // suffix
|
2018-11-01 21:43:40 +00:00
|
|
|
|
2018-01-23 11:34:40 +00:00
|
|
|
// Powers of 2 with a margin of +15/-16
|
2018-01-16 13:01:07 +00:00
|
|
|
if ((f.eq("1") || f.eq("2") || f.eq("4") || f.eq("8")) && m.chars().all(|c| c == '0'))
|
|
|
|
|| ((f.eq("1") || f.eq("3") || f.eq("7") || f.eq("F")) && m.chars().all(|c| c == 'F'))
|
|
|
|
// Lint for representations with only 0s and Fs, while allowing 7 as the first
|
|
|
|
// digit
|
|
|
|
|| ((f.eq("7") || f.eq("F")) && s.chars().all(|c| c == '0' || c == 'F'))
|
|
|
|
{
|
2018-01-23 14:29:31 +00:00
|
|
|
return Err(WarningType::DecimalRepresentation);
|
2018-01-16 13:01:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|