2017-07-19 18:56:32 -05:00
|
|
|
//! Lints concerned with the grouping of digits with underscores in integral or
|
|
|
|
//! floating-point literal expressions.
|
|
|
|
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::source::snippet_opt;
|
|
|
|
use clippy_utils::{
|
2020-03-03 10:58:37 -06:00
|
|
|
in_macro,
|
|
|
|
numeric_literal::{NumericLiteral, Radix},
|
|
|
|
};
|
2018-10-23 16:54:27 -05:00
|
|
|
use if_chain::if_chain;
|
2020-03-03 10:58:37 -06:00
|
|
|
use rustc_ast::ast::{Expr, ExprKind, Lit, LitKind};
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc_errors::Applicability;
|
2021-08-12 04:16:25 -05:00
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
2020-03-30 04:02:14 -05:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
2020-12-06 08:01:03 -06:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-03-08 17:57:44 -06:00
|
|
|
use std::iter;
|
2017-07-19 18:56:32 -05:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Warns if a long integral or floating-point constant does
|
2019-03-05 10:50:33 -06:00
|
|
|
/// not contain underscores.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Reading long numbers is difficult without separators.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2020-06-09 09:36:01 -05:00
|
|
|
/// // Bad
|
2019-03-05 16:23:50 -06:00
|
|
|
/// let x: u64 = 61864918973511;
|
2020-06-09 09:36:01 -05:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// let x: u64 = 61_864_918_973_511;
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2017-07-19 18:56:32 -05:00
|
|
|
pub UNREADABLE_LITERAL,
|
2020-04-04 14:47:16 -05:00
|
|
|
pedantic,
|
2020-12-06 08:01:03 -06:00
|
|
|
"long literal without underscores"
|
2017-07-19 18:56:32 -05:00
|
|
|
}
|
|
|
|
|
2018-09-02 16:07:55 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Warns for mistyped suffix in literals
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// This is most probably a typo
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Known problems
|
2019-10-03 15:07:51 -05:00
|
|
|
/// - Recommends a signed suffix, even though the number might be too big and an unsigned
|
|
|
|
/// suffix is required
|
2019-11-25 18:30:46 -06:00
|
|
|
/// - Does not match on `_127` since that is a valid grouping for decimal and octal numbers
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2020-06-09 09:36:01 -05:00
|
|
|
/// // Probably mistyped
|
2019-03-05 16:23:50 -06:00
|
|
|
/// 2_32;
|
2020-06-09 09:36:01 -05:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// 2_i32;
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2018-09-02 16:07:55 -05:00
|
|
|
pub MISTYPED_LITERAL_SUFFIXES,
|
|
|
|
correctness,
|
|
|
|
"mistyped literal suffix"
|
|
|
|
}
|
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Warns if an integral or floating-point constant is
|
2019-03-05 10:50:33 -06:00
|
|
|
/// grouped inconsistently with underscores.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Readers may incorrectly interpret inconsistently
|
2019-03-05 10:50:33 -06:00
|
|
|
/// grouped digits.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2020-06-09 09:36:01 -05:00
|
|
|
/// // Bad
|
2019-03-05 16:23:50 -06:00
|
|
|
/// let x: u64 = 618_64_9189_73_511;
|
2020-06-09 09:36:01 -05:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// let x: u64 = 61_864_918_973_511;
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2017-07-19 18:56:32 -05:00
|
|
|
pub INCONSISTENT_DIGIT_GROUPING,
|
2018-03-28 08:24:26 -05:00
|
|
|
style,
|
2017-07-19 18:56:32 -05:00
|
|
|
"integer literals with digits grouped inconsistently"
|
|
|
|
}
|
|
|
|
|
2020-10-28 17:36:07 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Warns if hexadecimal or binary literals are not grouped
|
2020-10-28 17:36:07 -05:00
|
|
|
/// by nibble or byte.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Negatively impacts readability.
|
2020-10-28 17:36:07 -05:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2020-10-28 17:36:07 -05:00
|
|
|
/// ```rust
|
|
|
|
/// let x: u32 = 0xFFF_FFF;
|
|
|
|
/// let y: u8 = 0b01_011_101;
|
|
|
|
/// ```
|
|
|
|
pub UNUSUAL_BYTE_GROUPINGS,
|
|
|
|
style,
|
|
|
|
"binary or hex literals that aren't grouped by four"
|
|
|
|
}
|
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Warns if the digits of an integral or floating-point
|
2019-03-05 10:50:33 -06:00
|
|
|
/// constant are grouped into groups that
|
|
|
|
/// are too large.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Negatively impacts readability.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2019-03-05 16:23:50 -06:00
|
|
|
/// let x: u64 = 6186491_8973511;
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2017-07-19 18:56:32 -05:00
|
|
|
pub LARGE_DIGIT_GROUPS,
|
2018-12-01 18:46:03 -06:00
|
|
|
pedantic,
|
2017-07-19 18:56:32 -05:00
|
|
|
"grouping digits into groups that are too large"
|
|
|
|
}
|
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Warns if there is a better representation for a numeric literal.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Especially for big powers of 2 a hexadecimal representation is more
|
2019-03-05 10:50:33 -06:00
|
|
|
/// readable than a decimal representation.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// `255` => `0xFF`
|
|
|
|
/// `65_535` => `0xFFFF`
|
|
|
|
/// `4_042_322_160` => `0xF0F0_F0F0`
|
2018-01-23 08:29:31 -06:00
|
|
|
pub DECIMAL_LITERAL_REPRESENTATION,
|
2018-03-28 08:24:26 -05:00
|
|
|
restriction,
|
2018-01-16 07:01:07 -06:00
|
|
|
"using decimal representation when hexadecimal would be better"
|
|
|
|
}
|
|
|
|
|
2017-07-30 17:33:15 -05:00
|
|
|
enum WarningType {
|
|
|
|
UnreadableLiteral,
|
|
|
|
InconsistentDigitGrouping,
|
|
|
|
LargeDigitGroups,
|
2018-01-23 08:29:31 -06:00
|
|
|
DecimalRepresentation,
|
2018-10-23 16:54:27 -05:00
|
|
|
MistypedLiteralSuffix,
|
2020-10-28 17:36:07 -05:00
|
|
|
UnusualByteGroupings,
|
2017-07-30 17:33:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WarningType {
|
2019-12-30 18:17:56 -06:00
|
|
|
fn display(&self, suggested_format: String, cx: &EarlyContext<'_>, span: rustc_span::Span) {
|
2018-05-31 13:15:48 -05:00
|
|
|
match self {
|
2019-07-30 19:25:35 -05:00
|
|
|
Self::MistypedLiteralSuffix => span_lint_and_sugg(
|
2018-10-23 16:54:27 -05:00
|
|
|
cx,
|
|
|
|
MISTYPED_LITERAL_SUFFIXES,
|
|
|
|
span,
|
|
|
|
"mistyped literal suffix",
|
|
|
|
"did you mean to write",
|
2019-11-13 00:28:06 -06:00
|
|
|
suggested_format,
|
2018-11-27 09:59:39 -06:00
|
|
|
Applicability::MaybeIncorrect,
|
2018-10-23 16:54:27 -05:00
|
|
|
),
|
2019-07-30 19:25:35 -05:00
|
|
|
Self::UnreadableLiteral => span_lint_and_sugg(
|
2017-09-05 04:33:04 -05:00
|
|
|
cx,
|
|
|
|
UNREADABLE_LITERAL,
|
2018-05-31 13:15:48 -05:00
|
|
|
span,
|
2017-09-05 04:33:04 -05:00
|
|
|
"long literal lacking separators",
|
2018-03-01 09:15:41 -06:00
|
|
|
"consider",
|
2019-11-13 00:28:06 -06:00
|
|
|
suggested_format,
|
2018-11-27 08:13:57 -06:00
|
|
|
Applicability::MachineApplicable,
|
2017-09-05 04:33:04 -05:00
|
|
|
),
|
2019-07-30 19:25:35 -05:00
|
|
|
Self::LargeDigitGroups => span_lint_and_sugg(
|
2017-09-05 04:33:04 -05:00
|
|
|
cx,
|
|
|
|
LARGE_DIGIT_GROUPS,
|
2018-05-31 13:15:48 -05:00
|
|
|
span,
|
2017-09-05 04:33:04 -05:00
|
|
|
"digit groups should be smaller",
|
2018-03-01 09:15:41 -06:00
|
|
|
"consider",
|
2019-11-13 00:28:06 -06:00
|
|
|
suggested_format,
|
2018-11-27 08:13:57 -06:00
|
|
|
Applicability::MachineApplicable,
|
2017-09-05 04:33:04 -05:00
|
|
|
),
|
2019-07-30 19:25:35 -05:00
|
|
|
Self::InconsistentDigitGrouping => span_lint_and_sugg(
|
2017-09-05 04:33:04 -05:00
|
|
|
cx,
|
|
|
|
INCONSISTENT_DIGIT_GROUPING,
|
2018-05-31 13:15:48 -05:00
|
|
|
span,
|
2017-09-05 04:33:04 -05:00
|
|
|
"digits grouped inconsistently by underscores",
|
2018-03-01 09:15:41 -06:00
|
|
|
"consider",
|
2019-11-13 00:28:06 -06:00
|
|
|
suggested_format,
|
2018-11-27 08:13:57 -06:00
|
|
|
Applicability::MachineApplicable,
|
2017-09-05 04:33:04 -05:00
|
|
|
),
|
2019-07-30 19:25:35 -05:00
|
|
|
Self::DecimalRepresentation => span_lint_and_sugg(
|
2018-01-16 07:01:07 -06:00
|
|
|
cx,
|
2018-01-23 08:29:31 -06:00
|
|
|
DECIMAL_LITERAL_REPRESENTATION,
|
2018-05-31 13:15:48 -05:00
|
|
|
span,
|
2018-01-23 08:29:31 -06:00
|
|
|
"integer literal has a better hexadecimal representation",
|
2018-03-01 09:15:41 -06:00
|
|
|
"consider",
|
2019-11-13 00:28:06 -06:00
|
|
|
suggested_format,
|
2018-11-27 08:13:57 -06:00
|
|
|
Applicability::MachineApplicable,
|
2018-01-16 07:01:07 -06:00
|
|
|
),
|
2020-10-28 17:36:07 -05:00
|
|
|
Self::UnusualByteGroupings => span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
UNUSUAL_BYTE_GROUPINGS,
|
|
|
|
span,
|
|
|
|
"digits of hex or binary literal not grouped by four",
|
|
|
|
"consider",
|
|
|
|
suggested_format,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
),
|
2017-07-30 17:33:15 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-06 08:01:03 -06:00
|
|
|
#[allow(clippy::module_name_repetitions)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct LiteralDigitGrouping {
|
|
|
|
lint_fraction_readability: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(LiteralDigitGrouping => [
|
2019-04-08 15:43:55 -05:00
|
|
|
UNREADABLE_LITERAL,
|
|
|
|
INCONSISTENT_DIGIT_GROUPING,
|
|
|
|
LARGE_DIGIT_GROUPS,
|
|
|
|
MISTYPED_LITERAL_SUFFIXES,
|
2020-10-28 17:36:07 -05:00
|
|
|
UNUSUAL_BYTE_GROUPINGS,
|
2019-04-08 15:43:55 -05:00
|
|
|
]);
|
2017-07-19 18:56:32 -05:00
|
|
|
|
|
|
|
impl EarlyLintPass for LiteralDigitGrouping {
|
2018-07-23 06:01:12 -05:00
|
|
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
2021-08-12 04:16:25 -05:00
|
|
|
if in_external_macro(cx.sess, expr.span) {
|
2017-07-19 18:56:32 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ExprKind::Lit(ref lit) = expr.kind {
|
2021-06-03 01:41:37 -05:00
|
|
|
self.check_lit(cx, lit);
|
2017-07-19 18:56:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 19:24:37 -05:00
|
|
|
// Length of each UUID hyphenated group in hex digits.
|
|
|
|
const UUID_GROUP_LENS: [usize; 5] = [8, 4, 4, 4, 12];
|
|
|
|
|
2017-07-19 18:56:32 -05:00
|
|
|
impl LiteralDigitGrouping {
|
2020-12-06 08:01:03 -06:00
|
|
|
pub fn new(lint_fraction_readability: bool) -> Self {
|
|
|
|
Self {
|
|
|
|
lint_fraction_readability,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
|
2019-11-13 00:26:52 -06:00
|
|
|
if_chain! {
|
|
|
|
if let Some(src) = snippet_opt(cx, lit.span);
|
2021-04-08 10:50:13 -05:00
|
|
|
if let Some(mut num_lit) = NumericLiteral::from_lit(&src, lit);
|
2019-11-13 00:26:52 -06:00
|
|
|
then {
|
2019-11-13 00:28:01 -06:00
|
|
|
if !Self::check_for_mistyped_suffix(cx, lit.span, &mut num_lit) {
|
2019-11-13 00:27:54 -06:00
|
|
|
return;
|
|
|
|
}
|
2019-11-13 00:26:52 -06:00
|
|
|
|
2020-04-11 19:24:37 -05:00
|
|
|
if Self::is_literal_uuid_formatted(&mut num_lit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-13 00:27:05 -06:00
|
|
|
let result = (|| {
|
2019-11-13 00:27:14 -06:00
|
|
|
|
2020-12-06 08:01:03 -06:00
|
|
|
let integral_group_size = Self::get_group_size(num_lit.integer.split('_'), num_lit.radix, true)?;
|
2019-11-13 00:28:01 -06:00
|
|
|
if let Some(fraction) = num_lit.fraction {
|
2020-12-06 08:01:03 -06:00
|
|
|
let fractional_group_size = Self::get_group_size(
|
|
|
|
fraction.rsplit('_'),
|
|
|
|
num_lit.radix,
|
|
|
|
self.lint_fraction_readability)?;
|
2019-11-13 00:27:27 -06:00
|
|
|
|
|
|
|
let consistent = Self::parts_consistent(integral_group_size,
|
|
|
|
fractional_group_size,
|
2019-11-13 00:28:01 -06:00
|
|
|
num_lit.integer.len(),
|
2019-11-13 00:27:27 -06:00
|
|
|
fraction.len());
|
|
|
|
if !consistent {
|
|
|
|
return Err(WarningType::InconsistentDigitGrouping);
|
|
|
|
};
|
2019-11-13 00:27:05 -06:00
|
|
|
}
|
2020-10-28 17:36:07 -05:00
|
|
|
|
2019-11-13 00:27:05 -06:00
|
|
|
Ok(())
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
if let Err(warning_type) = result {
|
2019-11-14 00:08:24 -06:00
|
|
|
let should_warn = match warning_type {
|
|
|
|
| WarningType::UnreadableLiteral
|
|
|
|
| WarningType::InconsistentDigitGrouping
|
2020-10-28 17:36:07 -05:00
|
|
|
| WarningType::UnusualByteGroupings
|
2019-11-14 00:08:24 -06:00
|
|
|
| WarningType::LargeDigitGroups => {
|
|
|
|
!in_macro(lit.span)
|
|
|
|
}
|
|
|
|
WarningType::DecimalRepresentation | WarningType::MistypedLiteralSuffix => {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if should_warn {
|
2021-06-03 01:41:37 -05:00
|
|
|
warning_type.display(num_lit.format(), cx, lit.span);
|
2019-11-14 00:08:24 -06:00
|
|
|
}
|
2019-11-13 00:27:05 -06:00
|
|
|
}
|
2019-11-13 00:26:52 -06:00
|
|
|
}
|
|
|
|
}
|
2017-07-19 18:56:32 -05:00
|
|
|
}
|
|
|
|
|
2019-11-13 00:27:54 -06:00
|
|
|
// Returns `false` if the check fails
|
|
|
|
fn check_for_mistyped_suffix(
|
|
|
|
cx: &EarlyContext<'_>,
|
2019-12-30 18:17:56 -06:00
|
|
|
span: rustc_span::Span,
|
2019-11-13 00:28:01 -06:00
|
|
|
num_lit: &mut NumericLiteral<'_>,
|
2019-11-13 00:27:54 -06:00
|
|
|
) -> bool {
|
2019-11-13 00:28:01 -06:00
|
|
|
if num_lit.suffix.is_some() {
|
2019-11-13 00:27:54 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-13 00:28:01 -06:00
|
|
|
let (part, mistyped_suffixes, missing_char) = if let Some((_, exponent)) = &mut num_lit.exponent {
|
2019-11-13 00:27:54 -06:00
|
|
|
(exponent, &["32", "64"][..], 'f')
|
2020-10-09 05:45:29 -05:00
|
|
|
} else if num_lit.fraction.is_some() {
|
|
|
|
(&mut num_lit.integer, &["32", "64"][..], 'f')
|
2019-11-13 00:27:54 -06:00
|
|
|
} else {
|
2020-10-09 05:45:29 -05:00
|
|
|
(&mut num_lit.integer, &["8", "16", "32", "64"][..], 'i')
|
2019-11-13 00:27:54 -06: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 00:28:06 -06: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 00:27:54 -06:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 19:24:37 -05:00
|
|
|
/// Checks whether the numeric literal matches the formatting of a UUID.
|
|
|
|
///
|
|
|
|
/// Returns `true` if the radix is hexadecimal, and the groups match the
|
|
|
|
/// UUID format of 8-4-4-4-12.
|
|
|
|
fn is_literal_uuid_formatted(num_lit: &mut NumericLiteral<'_>) -> bool {
|
|
|
|
if num_lit.radix != Radix::Hexadecimal {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UUIDs should not have a fraction
|
|
|
|
if num_lit.fraction.is_some() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let group_sizes: Vec<usize> = num_lit.integer.split('_').map(str::len).collect();
|
|
|
|
if UUID_GROUP_LENS.len() == group_sizes.len() {
|
2021-03-08 17:57:44 -06:00
|
|
|
iter::zip(&UUID_GROUP_LENS, &group_sizes).all(|(&a, &b)| a == b)
|
2020-04-11 19:24:37 -05:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-30 17:54:56 -05: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 01:37:41 -05:00
|
|
|
#[must_use]
|
2019-11-13 00:27:42 -06:00
|
|
|
fn parts_consistent(
|
|
|
|
int_group_size: Option<usize>,
|
|
|
|
frac_group_size: Option<usize>,
|
|
|
|
int_size: usize,
|
|
|
|
frac_size: usize,
|
|
|
|
) -> bool {
|
2017-07-30 17:54:56 -05:00
|
|
|
match (int_group_size, frac_group_size) {
|
|
|
|
// No groups on either side of decimal point - trivially consistent.
|
2019-11-13 00:27:42 -06:00
|
|
|
(None, None) => true,
|
2017-07-30 17:54:56 -05:00
|
|
|
// Integral part has grouped digits, fractional part does not.
|
2019-11-13 00:27:42 -06:00
|
|
|
(Some(int_group_size), None) => frac_size <= int_group_size,
|
2017-07-30 17:54:56 -05:00
|
|
|
// Fractional part has grouped digits, integral part does not.
|
2019-11-13 00:27:42 -06:00
|
|
|
(None, Some(frac_group_size)) => int_size <= frac_group_size,
|
2017-07-30 17:54:56 -05:00
|
|
|
// Both parts have grouped digits. Groups should be the same size.
|
2019-11-13 00:27:42 -06:00
|
|
|
(Some(int_group_size), Some(frac_group_size)) => int_group_size == frac_group_size,
|
2017-07-19 18:56:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 00:27:42 -06:00
|
|
|
/// Returns the size of the digit groups (or None if ungrouped) if successful,
|
|
|
|
/// otherwise returns a `WarningType` for linting.
|
2020-12-06 08:01:03 -06:00
|
|
|
fn get_group_size<'a>(
|
|
|
|
groups: impl Iterator<Item = &'a str>,
|
|
|
|
radix: Radix,
|
|
|
|
lint_unreadable: bool,
|
|
|
|
) -> Result<Option<usize>, WarningType> {
|
2019-11-13 00:27:42 -06:00
|
|
|
let mut groups = groups.map(str::len);
|
|
|
|
|
|
|
|
let first = groups.next().expect("At least one group");
|
|
|
|
|
2020-10-28 17:36:07 -05:00
|
|
|
if (radix == Radix::Binary || radix == Radix::Hexadecimal) && groups.any(|i| i != 4 && i != 2) {
|
|
|
|
return Err(WarningType::UnusualByteGroupings);
|
|
|
|
}
|
|
|
|
|
2019-11-13 00:27:42 -06:00
|
|
|
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 18:56:32 -05:00
|
|
|
} else {
|
2019-11-13 00:27:42 -06:00
|
|
|
Ok(Some(second))
|
2017-07-19 18:56:32 -05:00
|
|
|
}
|
2020-12-06 08:01:03 -06:00
|
|
|
} else if first > 5 && lint_unreadable {
|
2019-11-13 00:27:42 -06:00
|
|
|
Err(WarningType::UnreadableLiteral)
|
2017-07-19 18:56:32 -05:00
|
|
|
} else {
|
2019-11-13 00:27:42 -06:00
|
|
|
Ok(None)
|
2017-07-19 18:56:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 07:01:07 -06:00
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
#[allow(clippy::module_name_repetitions)]
|
2018-01-16 07:01:07 -06:00
|
|
|
#[derive(Copy, Clone)]
|
2019-04-08 15:43:55 -05:00
|
|
|
pub struct DecimalLiteralRepresentation {
|
2018-01-23 05:34:40 -06:00
|
|
|
threshold: u64,
|
|
|
|
}
|
2018-01-16 07:01:07 -06:00
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
impl_lint_pass!(DecimalLiteralRepresentation => [DECIMAL_LITERAL_REPRESENTATION]);
|
2018-01-16 07:01:07 -06:00
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
impl EarlyLintPass for DecimalLiteralRepresentation {
|
2018-07-23 06:01:12 -05:00
|
|
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
2021-08-12 04:16:25 -05:00
|
|
|
if in_external_macro(cx.sess, expr.span) {
|
2018-01-16 07:01:07 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ExprKind::Lit(ref lit) = expr.kind {
|
2021-06-03 01:41:37 -05:00
|
|
|
self.check_lit(cx, lit);
|
2018-01-16 07:01:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
impl DecimalLiteralRepresentation {
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
2018-01-23 05:34:40 -06:00
|
|
|
pub fn new(threshold: u64) -> Self {
|
2018-10-23 16:54:27 -05:00
|
|
|
Self { threshold }
|
2018-01-23 05:34:40 -06:00
|
|
|
}
|
2018-07-23 06:01:12 -05:00
|
|
|
fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
|
2018-01-16 07:01:07 -06:00
|
|
|
// Lint integral literals.
|
|
|
|
if_chain! {
|
2019-10-31 02:13:08 -05:00
|
|
|
if let LitKind::Int(val, _) = lit.kind;
|
2018-01-16 07:01:07 -06:00
|
|
|
if let Some(src) = snippet_opt(cx, lit.span);
|
2021-04-08 10:50:13 -05:00
|
|
|
if let Some(num_lit) = NumericLiteral::from_lit(&src, lit);
|
2019-11-13 00:28:01 -06:00
|
|
|
if num_lit.radix == Radix::Decimal;
|
2019-04-10 14:05:56 -05:00
|
|
|
if val >= u128::from(self.threshold);
|
2018-01-16 07:01:07 -06:00
|
|
|
then {
|
2019-04-10 14:05:56 -05:00
|
|
|
let hex = format!("{:#X}", val);
|
2019-12-25 10:22:36 -06:00
|
|
|
let num_lit = NumericLiteral::new(&hex, num_lit.suffix, false);
|
2019-11-13 00:28:01 -06:00
|
|
|
let _ = Self::do_lint(num_lit.integer).map_err(|warning_type| {
|
2021-06-03 01:41:37 -05:00
|
|
|
warning_type.display(num_lit.format(), cx, lit.span);
|
2019-04-10 14:05:56 -05:00
|
|
|
});
|
2018-01-16 07:01:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_lint(digits: &str) -> Result<(), WarningType> {
|
2018-01-23 05:34:40 -06:00
|
|
|
if digits.len() == 1 {
|
|
|
|
// Lint for 1 digit literals, if someone really sets the threshold that low
|
2018-10-23 16:54:27 -05:00
|
|
|
if digits == "1"
|
|
|
|
|| digits == "2"
|
|
|
|
|| digits == "4"
|
|
|
|
|| digits == "8"
|
|
|
|
|| digits == "3"
|
|
|
|
|| digits == "7"
|
2018-01-23 05:34:40 -06:00
|
|
|
|| digits == "F"
|
|
|
|
{
|
2018-01-23 08:29:31 -06:00
|
|
|
return Err(WarningType::DecimalRepresentation);
|
2018-01-23 05:34:40 -06:00
|
|
|
}
|
|
|
|
} else if digits.len() < 4 {
|
|
|
|
// Lint for Literals with a hex-representation of 2 or 3 digits
|
2018-01-16 07:01:07 -06:00
|
|
|
let f = &digits[0..1]; // first digit
|
|
|
|
let s = &digits[1..]; // suffix
|
2018-11-01 16:43:40 -05:00
|
|
|
|
2018-01-23 05:34:40 -06: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 08:29:31 -06:00
|
|
|
return Err(WarningType::DecimalRepresentation);
|
2018-01-16 07:01:07 -06:00
|
|
|
}
|
2018-01-23 05:34:40 -06:00
|
|
|
} else {
|
2018-01-16 07:01:07 -06: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 16:43:40 -05:00
|
|
|
|
2018-01-23 05:34:40 -06:00
|
|
|
// Powers of 2 with a margin of +15/-16
|
2018-01-16 07:01:07 -06: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 08:29:31 -06:00
|
|
|
return Err(WarningType::DecimalRepresentation);
|
2018-01-16 07:01:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|