1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 //! Lints concerned with the grouping of digits with underscores in integral or
11 //! floating-point literal expressions.
13 use crate::rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};
14 use crate::rustc::{declare_tool_lint, lint_array};
15 use crate::syntax::ast::*;
16 use crate::syntax_pos;
17 use crate::utils::{snippet_opt, span_lint_and_sugg};
18 use if_chain::if_chain;
20 /// **What it does:** Warns if a long integral or floating-point constant does
21 /// not contain underscores.
23 /// **Why is this bad?** Reading long numbers is difficult without separators.
25 /// **Known problems:** None.
32 declare_clippy_lint! {
33 pub UNREADABLE_LITERAL,
35 "long integer literal without underscores"
38 /// **What it does:** Warns for mistyped suffix in literals
40 /// **Why is this bad?** This is most probably a typo
42 /// **Known problems:**
43 /// - Recommends a signed suffix, even though the number might be too big and an unsigned
44 /// suffix is required
45 /// - Does not match on `_128` since that is a valid grouping for decimal and octal numbers
52 declare_clippy_lint! {
53 pub MISTYPED_LITERAL_SUFFIXES,
55 "mistyped literal suffix"
58 /// **What it does:** Warns if an integral or floating-point constant is
59 /// grouped inconsistently with underscores.
61 /// **Why is this bad?** Readers may incorrectly interpret inconsistently
64 /// **Known problems:** None.
69 /// 618_64_9189_73_511
71 declare_clippy_lint! {
72 pub INCONSISTENT_DIGIT_GROUPING,
74 "integer literals with digits grouped inconsistently"
77 /// **What it does:** Warns if the digits of an integral or floating-point
78 /// constant are grouped into groups that
81 /// **Why is this bad?** Negatively impacts readability.
83 /// **Known problems:** None.
90 declare_clippy_lint! {
91 pub LARGE_DIGIT_GROUPS,
93 "grouping digits into groups that are too large"
96 /// **What it does:** Warns if there is a better representation for a numeric literal.
98 /// **Why is this bad?** Especially for big powers of 2 a hexadecimal representation is more
99 /// readable than a decimal representation.
101 /// **Known problems:** None.
106 /// `65_535` => `0xFFFF`
107 /// `4_042_322_160` => `0xF0F0_F0F0`
108 declare_clippy_lint! {
109 pub DECIMAL_LITERAL_REPRESENTATION,
111 "using decimal representation when hexadecimal would be better"
114 #[derive(Debug, PartialEq)]
115 pub(super) enum Radix {
123 /// Return a reasonable digit group size for this radix.
124 crate fn suggest_grouping(&self) -> usize {
126 Radix::Binary | Radix::Hexadecimal => 4,
127 Radix::Octal | Radix::Decimal => 3,
133 pub(super) struct DigitInfo<'a> {
134 /// Characters of a literal between the radix prefix and type suffix.
135 crate digits: &'a str,
136 /// Which radix the literal was represented in.
138 /// The radix prefix, if present.
139 crate prefix: Option<&'a str>,
140 /// The type suffix, including preceding underscore if present.
141 crate suffix: Option<&'a str>,
142 /// True for floating-point literals.
146 impl<'a> DigitInfo<'a> {
147 crate fn new(lit: &'a str, float: bool) -> Self {
148 // Determine delimiter for radix prefix, if present, and radix.
149 let radix = if lit.starts_with("0x") {
151 } else if lit.starts_with("0b") {
153 } else if lit.starts_with("0o") {
159 // Grab part of the literal after prefix, if present.
160 let (prefix, sans_prefix) = if let Radix::Decimal = radix {
163 let (p, s) = lit.split_at(2);
167 let len = sans_prefix.len();
168 let mut last_d = '\0';
169 for (d_idx, d) in sans_prefix.char_indices() {
170 let suffix_start = if last_d == '_' { d_idx - 1 } else { d_idx };
173 || is_possible_float_suffix_index(&sans_prefix, suffix_start, len)
174 || ((d == 'E' || d == 'e') && !has_possible_float_suffix(&sans_prefix)))
175 || !float && (d == 'i' || d == 'u' || is_possible_suffix_index(&sans_prefix, suffix_start, len))
177 let (digits, suffix) = sans_prefix.split_at(suffix_start);
182 suffix: Some(suffix),
199 /// Returns literal formatted in a sensible way.
200 crate fn grouping_hint(&self) -> String {
201 let group_size = self.radix.suggest_grouping();
202 if self.digits.contains('.') {
203 let mut parts = self.digits.split('.');
204 let int_part_hint = parts
206 .expect("split always returns at least one element")
209 .filter(|&c| c != '_')
212 .map(|chunk| chunk.into_iter().rev().collect())
214 .collect::<Vec<String>>()
216 let frac_part_hint = parts
218 .expect("already checked that there is a `.`")
220 .filter(|&c| c != '_')
223 .map(|chunk| chunk.into_iter().collect())
224 .collect::<Vec<String>>()
226 let suffix_hint = match self.suffix {
227 Some(suffix) if is_mistyped_float_suffix(suffix) => format!("_f{}", &suffix[1..]),
228 Some(suffix) => suffix.to_string(),
229 None => String::new(),
231 format!("{}.{}{}", int_part_hint, frac_part_hint, suffix_hint)
232 } else if self.float && (self.digits.contains('E') || self.digits.contains('e')) {
233 let which_e = if self.digits.contains('E') { 'E' } else { 'e' };
234 let parts: Vec<&str> = self.digits.split(which_e).collect();
235 let filtered_digits_vec_0 = parts[0].chars().filter(|&c| c != '_').rev().collect::<Vec<_>>();
236 let filtered_digits_vec_1 = parts[1].chars().filter(|&c| c != '_').rev().collect::<Vec<_>>();
237 let before_e_hint = filtered_digits_vec_0
239 .map(|chunk| chunk.into_iter().rev().collect())
241 .collect::<Vec<String>>()
243 let after_e_hint = filtered_digits_vec_1
245 .map(|chunk| chunk.into_iter().rev().collect())
247 .collect::<Vec<String>>()
249 let suffix_hint = match self.suffix {
250 Some(suffix) if is_mistyped_float_suffix(suffix) => format!("_f{}", &suffix[1..]),
251 Some(suffix) => suffix.to_string(),
252 None => String::new(),
256 self.prefix.unwrap_or(""),
263 let filtered_digits_vec = self.digits.chars().filter(|&c| c != '_').rev().collect::<Vec<_>>();
264 let mut hint = filtered_digits_vec
266 .map(|chunk| chunk.into_iter().rev().collect())
268 .collect::<Vec<String>>()
270 // Forces hexadecimal values to be grouped by 4 being filled with zeroes (e.g 0x00ab_cdef)
271 let nb_digits_to_fill = filtered_digits_vec.len() % 4;
272 if self.radix == Radix::Hexadecimal && nb_digits_to_fill != 0 {
273 hint = format!("{:0>4}{}", &hint[..nb_digits_to_fill], &hint[nb_digits_to_fill..]);
275 let suffix_hint = match self.suffix {
276 Some(suffix) if is_mistyped_suffix(suffix) => format!("_i{}", &suffix[1..]),
277 Some(suffix) => suffix.to_string(),
278 None => String::new(),
280 format!("{}{}{}", self.prefix.unwrap_or(""), hint, suffix_hint)
287 InconsistentDigitGrouping,
289 DecimalRepresentation,
290 MistypedLiteralSuffix,
294 crate fn display(&self, grouping_hint: &str, cx: &EarlyContext<'_>, span: syntax_pos::Span) {
296 WarningType::MistypedLiteralSuffix => span_lint_and_sugg(
298 MISTYPED_LITERAL_SUFFIXES,
300 "mistyped literal suffix",
301 "did you mean to write",
302 grouping_hint.to_string(),
304 WarningType::UnreadableLiteral => span_lint_and_sugg(
308 "long literal lacking separators",
310 grouping_hint.to_owned(),
312 WarningType::LargeDigitGroups => span_lint_and_sugg(
316 "digit groups should be smaller",
318 grouping_hint.to_owned(),
320 WarningType::InconsistentDigitGrouping => span_lint_and_sugg(
322 INCONSISTENT_DIGIT_GROUPING,
324 "digits grouped inconsistently by underscores",
326 grouping_hint.to_owned(),
328 WarningType::DecimalRepresentation => span_lint_and_sugg(
330 DECIMAL_LITERAL_REPRESENTATION,
332 "integer literal has a better hexadecimal representation",
334 grouping_hint.to_owned(),
340 #[derive(Copy, Clone)]
341 pub struct LiteralDigitGrouping;
343 impl LintPass for LiteralDigitGrouping {
344 fn get_lints(&self) -> LintArray {
345 lint_array!(UNREADABLE_LITERAL, INCONSISTENT_DIGIT_GROUPING, LARGE_DIGIT_GROUPS)
349 impl EarlyLintPass for LiteralDigitGrouping {
350 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
351 if in_external_macro(cx.sess(), expr.span) {
355 if let ExprKind::Lit(ref lit) = expr.node {
356 self.check_lit(cx, lit)
361 impl LiteralDigitGrouping {
362 fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
364 LitKind::Int(..) => {
365 // Lint integral literals.
367 if let Some(src) = snippet_opt(cx, lit.span);
368 if let Some(firstch) = src.chars().next();
369 if char::to_digit(firstch, 10).is_some();
371 let digit_info = DigitInfo::new(&src, false);
372 let _ = Self::do_lint(digit_info.digits, digit_info.suffix).map_err(|warning_type| {
373 warning_type.display(&digit_info.grouping_hint(), cx, lit.span)
378 LitKind::Float(..) | LitKind::FloatUnsuffixed(..) => {
379 // Lint floating-point literals.
381 if let Some(src) = snippet_opt(cx, lit.span);
382 if let Some(firstch) = src.chars().next();
383 if char::to_digit(firstch, 10).is_some();
385 let digit_info = DigitInfo::new(&src, true);
386 // Separate digits into integral and fractional parts.
387 let parts: Vec<&str> = digit_info
389 .split_terminator('.')
392 // Lint integral and fractional parts separately, and then check consistency of digit
393 // groups if both pass.
394 let _ = Self::do_lint(parts[0], digit_info.suffix)
395 .map(|integral_group_size| {
397 // Lint the fractional part of literal just like integral part, but reversed.
398 let fractional_part = &parts[1].chars().rev().collect::<String>();
399 let _ = Self::do_lint(fractional_part, None)
400 .map(|fractional_group_size| {
401 let consistent = Self::parts_consistent(integral_group_size,
402 fractional_group_size,
406 WarningType::InconsistentDigitGrouping.display(&digit_info.grouping_hint(),
411 .map_err(|warning_type| warning_type.display(&digit_info.grouping_hint(),
416 .map_err(|warning_type| warning_type.display(&digit_info.grouping_hint(), cx, lit.span));
424 /// Given the sizes of the digit groups of both integral and fractional
425 /// parts, and the length
426 /// of both parts, determine if the digits have been grouped consistently.
427 fn parts_consistent(int_group_size: usize, frac_group_size: usize, int_size: usize, frac_size: usize) -> bool {
428 match (int_group_size, frac_group_size) {
429 // No groups on either side of decimal point - trivially consistent.
431 // Integral part has grouped digits, fractional part does not.
432 (_, 0) => frac_size <= int_group_size,
433 // Fractional part has grouped digits, integral part does not.
434 (0, _) => int_size <= frac_group_size,
435 // Both parts have grouped digits. Groups should be the same size.
436 (_, _) => int_group_size == frac_group_size,
440 /// Performs lint on `digits` (no decimal point) and returns the group
441 /// size on success or `WarningType` when emitting a warning.
442 fn do_lint(digits: &str, suffix: Option<&str>) -> Result<usize, WarningType> {
443 if let Some(suffix) = suffix {
444 if is_mistyped_suffix(suffix) {
445 return Err(WarningType::MistypedLiteralSuffix);
448 // Grab underscore indices with respect to the units digit.
449 let underscore_positions: Vec<usize> = digits
453 .filter_map(|(idx, digit)| if digit == '_' { Some(idx) } else { None })
456 if underscore_positions.is_empty() {
457 // Check if literal needs underscores.
458 if digits.len() > 5 {
459 Err(WarningType::UnreadableLiteral)
464 // Check consistency and the sizes of the groups.
465 let group_size = underscore_positions[0];
466 let consistent = underscore_positions
468 .all(|ps| ps[1] - ps[0] == group_size + 1)
469 // number of digits to the left of the last group cannot be bigger than group size.
470 && (digits.len() - underscore_positions.last()
471 .expect("there's at least one element") <= group_size + 1);
474 return Err(WarningType::InconsistentDigitGrouping);
475 } else if group_size > 4 {
476 return Err(WarningType::LargeDigitGroups);
483 #[derive(Copy, Clone)]
484 pub struct LiteralRepresentation {
488 impl LintPass for LiteralRepresentation {
489 fn get_lints(&self) -> LintArray {
490 lint_array!(DECIMAL_LITERAL_REPRESENTATION)
494 impl EarlyLintPass for LiteralRepresentation {
495 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
496 if in_external_macro(cx.sess(), expr.span) {
500 if let ExprKind::Lit(ref lit) = expr.node {
501 self.check_lit(cx, lit)
506 impl LiteralRepresentation {
507 pub fn new(threshold: u64) -> Self {
510 fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
511 // Lint integral literals.
513 if let LitKind::Int(..) = lit.node;
514 if let Some(src) = snippet_opt(cx, lit.span);
515 if let Some(firstch) = src.chars().next();
516 if char::to_digit(firstch, 10).is_some();
518 let digit_info = DigitInfo::new(&src, false);
519 if digit_info.radix == Radix::Decimal {
520 let val = digit_info.digits
522 .filter(|&c| c != '_')
524 .parse::<u128>().unwrap();
525 if val < u128::from(self.threshold) {
528 let hex = format!("{:#X}", val);
529 let digit_info = DigitInfo::new(&hex[..], false);
530 let _ = Self::do_lint(digit_info.digits).map_err(|warning_type| {
531 warning_type.display(&digit_info.grouping_hint(), cx, lit.span)
538 fn do_lint(digits: &str) -> Result<(), WarningType> {
539 if digits.len() == 1 {
540 // Lint for 1 digit literals, if someone really sets the threshold that low
549 return Err(WarningType::DecimalRepresentation);
551 } else if digits.len() < 4 {
552 // Lint for Literals with a hex-representation of 2 or 3 digits
553 let f = &digits[0..1]; // first digit
554 let s = &digits[1..]; // suffix
556 if ((f.eq("1") || f.eq("2") || f.eq("4") || f.eq("8")) && s.chars().all(|c| c == '0'))
557 // Powers of 2 minus 1
558 || ((f.eq("1") || f.eq("3") || f.eq("7") || f.eq("F")) && s.chars().all(|c| c == 'F'))
560 return Err(WarningType::DecimalRepresentation);
563 // Lint for Literals with a hex-representation of 4 digits or more
564 let f = &digits[0..1]; // first digit
565 let m = &digits[1..digits.len() - 1]; // middle digits, except last
566 let s = &digits[1..]; // suffix
567 // Powers of 2 with a margin of +15/-16
568 if ((f.eq("1") || f.eq("2") || f.eq("4") || f.eq("8")) && m.chars().all(|c| c == '0'))
569 || ((f.eq("1") || f.eq("3") || f.eq("7") || f.eq("F")) && m.chars().all(|c| c == 'F'))
570 // Lint for representations with only 0s and Fs, while allowing 7 as the first
572 || ((f.eq("7") || f.eq("F")) && s.chars().all(|c| c == '0' || c == 'F'))
574 return Err(WarningType::DecimalRepresentation);
582 fn is_mistyped_suffix(suffix: &str) -> bool {
583 ["_8", "_16", "_32", "_64"].contains(&suffix)
586 fn is_possible_suffix_index(lit: &str, idx: usize, len: usize) -> bool {
587 ((len > 3 && idx == len - 3) || (len > 2 && idx == len - 2)) && is_mistyped_suffix(lit.split_at(idx).1)
590 fn is_mistyped_float_suffix(suffix: &str) -> bool {
591 ["_32", "_64"].contains(&suffix)
594 fn is_possible_float_suffix_index(lit: &str, idx: usize, len: usize) -> bool {
595 (len > 3 && idx == len - 3) && is_mistyped_float_suffix(lit.split_at(idx).1)
598 fn has_possible_float_suffix(lit: &str) -> bool {
599 lit.ends_with("_32") || lit.ends_with("_64")