]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/literal_representation.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[rust.git] / clippy_lints / src / literal_representation.rs
1 //! Lints concerned with the grouping of digits with underscores in integral or
2 //! floating-point literal expressions.
3
4 use crate::utils::{snippet_opt, span_lint_and_sugg};
5 use if_chain::if_chain;
6 use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};
7 use rustc::{declare_tool_lint, lint_array};
8 use rustc_errors::Applicability;
9 use syntax::ast::*;
10 use syntax_pos;
11
12 declare_clippy_lint! {
13     /// **What it does:** Warns if a long integral or floating-point constant does
14     /// not contain underscores.
15     ///
16     /// **Why is this bad?** Reading long numbers is difficult without separators.
17     ///
18     /// **Known problems:** None.
19     ///
20     /// **Example:**
21     ///
22     /// ```rust
23     /// let x: u64 = 61864918973511;
24     /// ```
25     pub UNREADABLE_LITERAL,
26     style,
27     "long integer literal without underscores"
28 }
29
30 declare_clippy_lint! {
31     /// **What it does:** Warns for mistyped suffix in literals
32     ///
33     /// **Why is this bad?** This is most probably a typo
34     ///
35     /// **Known problems:**
36     ///                 - Recommends a signed suffix, even though the number might be too big and an unsigned
37     ///         suffix is required
38     ///                 - Does not match on `_128` since that is a valid grouping for decimal and octal numbers
39     ///
40     /// **Example:**
41     ///
42     /// ```rust
43     /// 2_32;
44     /// ```
45     pub MISTYPED_LITERAL_SUFFIXES,
46     correctness,
47     "mistyped literal suffix"
48 }
49
50 declare_clippy_lint! {
51     /// **What it does:** Warns if an integral or floating-point constant is
52     /// grouped inconsistently with underscores.
53     ///
54     /// **Why is this bad?** Readers may incorrectly interpret inconsistently
55     /// grouped digits.
56     ///
57     /// **Known problems:** None.
58     ///
59     /// **Example:**
60     ///
61     /// ```rust
62     /// let x: u64 = 618_64_9189_73_511;
63     /// ```
64     pub INCONSISTENT_DIGIT_GROUPING,
65     style,
66     "integer literals with digits grouped inconsistently"
67 }
68
69 declare_clippy_lint! {
70     /// **What it does:** Warns if the digits of an integral or floating-point
71     /// constant are grouped into groups that
72     /// are too large.
73     ///
74     /// **Why is this bad?** Negatively impacts readability.
75     ///
76     /// **Known problems:** None.
77     ///
78     /// **Example:**
79     ///
80     /// ```rust
81     /// let x: u64 = 6186491_8973511;
82     /// ```
83     pub LARGE_DIGIT_GROUPS,
84     pedantic,
85     "grouping digits into groups that are too large"
86 }
87
88 declare_clippy_lint! {
89     /// **What it does:** Warns if there is a better representation for a numeric literal.
90     ///
91     /// **Why is this bad?** Especially for big powers of 2 a hexadecimal representation is more
92     /// readable than a decimal representation.
93     ///
94     /// **Known problems:** None.
95     ///
96     /// **Example:**
97     ///
98     /// `255` => `0xFF`
99     /// `65_535` => `0xFFFF`
100     /// `4_042_322_160` => `0xF0F0_F0F0`
101     pub DECIMAL_LITERAL_REPRESENTATION,
102     restriction,
103     "using decimal representation when hexadecimal would be better"
104 }
105
106 #[derive(Debug, PartialEq)]
107 pub(super) enum Radix {
108     Binary,
109     Octal,
110     Decimal,
111     Hexadecimal,
112 }
113
114 impl Radix {
115     /// Returns a reasonable digit group size for this radix.
116     crate fn suggest_grouping(&self) -> usize {
117         match *self {
118             Radix::Binary | Radix::Hexadecimal => 4,
119             Radix::Octal | Radix::Decimal => 3,
120         }
121     }
122 }
123
124 #[derive(Debug)]
125 pub(super) struct DigitInfo<'a> {
126     /// Characters of a literal between the radix prefix and type suffix.
127     crate digits: &'a str,
128     /// Which radix the literal was represented in.
129     crate radix: Radix,
130     /// The radix prefix, if present.
131     crate prefix: Option<&'a str>,
132     /// The type suffix, including preceding underscore if present.
133     crate suffix: Option<&'a str>,
134     /// True for floating-point literals.
135     crate float: bool,
136 }
137
138 impl<'a> DigitInfo<'a> {
139     crate fn new(lit: &'a str, float: bool) -> Self {
140         // Determine delimiter for radix prefix, if present, and radix.
141         let radix = if lit.starts_with("0x") {
142             Radix::Hexadecimal
143         } else if lit.starts_with("0b") {
144             Radix::Binary
145         } else if lit.starts_with("0o") {
146             Radix::Octal
147         } else {
148             Radix::Decimal
149         };
150
151         // Grab part of the literal after prefix, if present.
152         let (prefix, sans_prefix) = if let Radix::Decimal = radix {
153             (None, lit)
154         } else {
155             let (p, s) = lit.split_at(2);
156             (Some(p), s)
157         };
158
159         let len = sans_prefix.len();
160         let mut last_d = '\0';
161         for (d_idx, d) in sans_prefix.char_indices() {
162             let suffix_start = if last_d == '_' { d_idx - 1 } else { d_idx };
163             if float
164                 && (d == 'f'
165                     || is_possible_float_suffix_index(&sans_prefix, suffix_start, len)
166                     || ((d == 'E' || d == 'e') && !has_possible_float_suffix(&sans_prefix)))
167                 || !float && (d == 'i' || d == 'u' || is_possible_suffix_index(&sans_prefix, suffix_start, len))
168             {
169                 let (digits, suffix) = sans_prefix.split_at(suffix_start);
170                 return Self {
171                     digits,
172                     radix,
173                     prefix,
174                     suffix: Some(suffix),
175                     float,
176                 };
177             }
178             last_d = d
179         }
180
181         // No suffix found
182         Self {
183             digits: sans_prefix,
184             radix,
185             prefix,
186             suffix: None,
187             float,
188         }
189     }
190
191     /// Returns literal formatted in a sensible way.
192     crate fn grouping_hint(&self) -> String {
193         let group_size = self.radix.suggest_grouping();
194         if self.digits.contains('.') {
195             let mut parts = self.digits.split('.');
196             let int_part_hint = parts
197                 .next()
198                 .expect("split always returns at least one element")
199                 .chars()
200                 .rev()
201                 .filter(|&c| c != '_')
202                 .collect::<Vec<_>>()
203                 .chunks(group_size)
204                 .map(|chunk| chunk.iter().rev().collect())
205                 .rev()
206                 .collect::<Vec<String>>()
207                 .join("_");
208             let frac_part_hint = parts
209                 .next()
210                 .expect("already checked that there is a `.`")
211                 .chars()
212                 .filter(|&c| c != '_')
213                 .collect::<Vec<_>>()
214                 .chunks(group_size)
215                 .map(|chunk| chunk.iter().collect())
216                 .collect::<Vec<String>>()
217                 .join("_");
218             let suffix_hint = match self.suffix {
219                 Some(suffix) if is_mistyped_float_suffix(suffix) => format!("_f{}", &suffix[1..]),
220                 Some(suffix) => suffix.to_string(),
221                 None => String::new(),
222             };
223             format!("{}.{}{}", int_part_hint, frac_part_hint, suffix_hint)
224         } else if self.float && (self.digits.contains('E') || self.digits.contains('e')) {
225             let which_e = if self.digits.contains('E') { 'E' } else { 'e' };
226             let parts: Vec<&str> = self.digits.split(which_e).collect();
227             let filtered_digits_vec_0 = parts[0].chars().filter(|&c| c != '_').rev().collect::<Vec<_>>();
228             let filtered_digits_vec_1 = parts[1].chars().filter(|&c| c != '_').rev().collect::<Vec<_>>();
229             let before_e_hint = filtered_digits_vec_0
230                 .chunks(group_size)
231                 .map(|chunk| chunk.iter().rev().collect())
232                 .rev()
233                 .collect::<Vec<String>>()
234                 .join("_");
235             let after_e_hint = filtered_digits_vec_1
236                 .chunks(group_size)
237                 .map(|chunk| chunk.iter().rev().collect())
238                 .rev()
239                 .collect::<Vec<String>>()
240                 .join("_");
241             let suffix_hint = match self.suffix {
242                 Some(suffix) if is_mistyped_float_suffix(suffix) => format!("_f{}", &suffix[1..]),
243                 Some(suffix) => suffix.to_string(),
244                 None => String::new(),
245             };
246             format!(
247                 "{}{}{}{}{}",
248                 self.prefix.unwrap_or(""),
249                 before_e_hint,
250                 which_e,
251                 after_e_hint,
252                 suffix_hint
253             )
254         } else {
255             let filtered_digits_vec = self.digits.chars().filter(|&c| c != '_').rev().collect::<Vec<_>>();
256             let mut hint = filtered_digits_vec
257                 .chunks(group_size)
258                 .map(|chunk| chunk.iter().rev().collect())
259                 .rev()
260                 .collect::<Vec<String>>()
261                 .join("_");
262             // Forces hexadecimal values to be grouped by 4 being filled with zeroes (e.g 0x00ab_cdef)
263             let nb_digits_to_fill = filtered_digits_vec.len() % 4;
264             if self.radix == Radix::Hexadecimal && nb_digits_to_fill != 0 {
265                 hint = format!("{:0>4}{}", &hint[..nb_digits_to_fill], &hint[nb_digits_to_fill..]);
266             }
267             let suffix_hint = match self.suffix {
268                 Some(suffix) if is_mistyped_suffix(suffix) => format!("_i{}", &suffix[1..]),
269                 Some(suffix) => suffix.to_string(),
270                 None => String::new(),
271             };
272             format!("{}{}{}", self.prefix.unwrap_or(""), hint, suffix_hint)
273         }
274     }
275 }
276
277 enum WarningType {
278     UnreadableLiteral,
279     InconsistentDigitGrouping,
280     LargeDigitGroups,
281     DecimalRepresentation,
282     MistypedLiteralSuffix,
283 }
284
285 impl WarningType {
286     crate fn display(&self, grouping_hint: &str, cx: &EarlyContext<'_>, span: syntax_pos::Span) {
287         match self {
288             WarningType::MistypedLiteralSuffix => span_lint_and_sugg(
289                 cx,
290                 MISTYPED_LITERAL_SUFFIXES,
291                 span,
292                 "mistyped literal suffix",
293                 "did you mean to write",
294                 grouping_hint.to_string(),
295                 Applicability::MaybeIncorrect,
296             ),
297             WarningType::UnreadableLiteral => span_lint_and_sugg(
298                 cx,
299                 UNREADABLE_LITERAL,
300                 span,
301                 "long literal lacking separators",
302                 "consider",
303                 grouping_hint.to_owned(),
304                 Applicability::MachineApplicable,
305             ),
306             WarningType::LargeDigitGroups => span_lint_and_sugg(
307                 cx,
308                 LARGE_DIGIT_GROUPS,
309                 span,
310                 "digit groups should be smaller",
311                 "consider",
312                 grouping_hint.to_owned(),
313                 Applicability::MachineApplicable,
314             ),
315             WarningType::InconsistentDigitGrouping => span_lint_and_sugg(
316                 cx,
317                 INCONSISTENT_DIGIT_GROUPING,
318                 span,
319                 "digits grouped inconsistently by underscores",
320                 "consider",
321                 grouping_hint.to_owned(),
322                 Applicability::MachineApplicable,
323             ),
324             WarningType::DecimalRepresentation => span_lint_and_sugg(
325                 cx,
326                 DECIMAL_LITERAL_REPRESENTATION,
327                 span,
328                 "integer literal has a better hexadecimal representation",
329                 "consider",
330                 grouping_hint.to_owned(),
331                 Applicability::MachineApplicable,
332             ),
333         };
334     }
335 }
336
337 #[derive(Copy, Clone)]
338 pub struct LiteralDigitGrouping;
339
340 impl LintPass for LiteralDigitGrouping {
341     fn get_lints(&self) -> LintArray {
342         lint_array!(
343             UNREADABLE_LITERAL,
344             INCONSISTENT_DIGIT_GROUPING,
345             LARGE_DIGIT_GROUPS,
346             MISTYPED_LITERAL_SUFFIXES,
347         )
348     }
349
350     fn name(&self) -> &'static str {
351         "LiteralDigitGrouping"
352     }
353 }
354
355 impl EarlyLintPass for LiteralDigitGrouping {
356     fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
357         if in_external_macro(cx.sess(), expr.span) {
358             return;
359         }
360
361         if let ExprKind::Lit(ref lit) = expr.node {
362             self.check_lit(cx, lit)
363         }
364     }
365 }
366
367 impl LiteralDigitGrouping {
368     fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
369         match lit.node {
370             LitKind::Int(..) => {
371                 // Lint integral literals.
372                 if_chain! {
373                     if let Some(src) = snippet_opt(cx, lit.span);
374                     if let Some(firstch) = src.chars().next();
375                     if char::to_digit(firstch, 10).is_some();
376                     then {
377                         let digit_info = DigitInfo::new(&src, false);
378                         let _ = Self::do_lint(digit_info.digits, digit_info.suffix).map_err(|warning_type| {
379                             warning_type.display(&digit_info.grouping_hint(), cx, lit.span)
380                         });
381                     }
382                 }
383             },
384             LitKind::Float(..) | LitKind::FloatUnsuffixed(..) => {
385                 // Lint floating-point literals.
386                 if_chain! {
387                     if let Some(src) = snippet_opt(cx, lit.span);
388                     if let Some(firstch) = src.chars().next();
389                     if char::to_digit(firstch, 10).is_some();
390                     then {
391                         let digit_info = DigitInfo::new(&src, true);
392                         // Separate digits into integral and fractional parts.
393                         let parts: Vec<&str> = digit_info
394                             .digits
395                             .split_terminator('.')
396                             .collect();
397
398                         // Lint integral and fractional parts separately, and then check consistency of digit
399                         // groups if both pass.
400                         let _ = Self::do_lint(parts[0], digit_info.suffix)
401                             .map(|integral_group_size| {
402                                 if parts.len() > 1 {
403                                     // Lint the fractional part of literal just like integral part, but reversed.
404                                     let fractional_part = &parts[1].chars().rev().collect::<String>();
405                                     let _ = Self::do_lint(fractional_part, None)
406                                         .map(|fractional_group_size| {
407                                             let consistent = Self::parts_consistent(integral_group_size,
408                                                                                     fractional_group_size,
409                                                                                     parts[0].len(),
410                                                                                     parts[1].len());
411                                                 if !consistent {
412                                                     WarningType::InconsistentDigitGrouping.display(
413                                                         &digit_info.grouping_hint(),
414                                                         cx,
415                                                         lit.span,
416                                                     );
417                                                 }
418                                         })
419                                     .map_err(|warning_type| warning_type.display(&digit_info.grouping_hint(),
420                                     cx,
421                                     lit.span));
422                                 }
423                             })
424                         .map_err(|warning_type| warning_type.display(&digit_info.grouping_hint(), cx, lit.span));
425                     }
426                 }
427             },
428             _ => (),
429         }
430     }
431
432     /// Given the sizes of the digit groups of both integral and fractional
433     /// parts, and the length
434     /// of both parts, determine if the digits have been grouped consistently.
435     fn parts_consistent(int_group_size: usize, frac_group_size: usize, int_size: usize, frac_size: usize) -> bool {
436         match (int_group_size, frac_group_size) {
437             // No groups on either side of decimal point - trivially consistent.
438             (0, 0) => true,
439             // Integral part has grouped digits, fractional part does not.
440             (_, 0) => frac_size <= int_group_size,
441             // Fractional part has grouped digits, integral part does not.
442             (0, _) => int_size <= frac_group_size,
443             // Both parts have grouped digits. Groups should be the same size.
444             (_, _) => int_group_size == frac_group_size,
445         }
446     }
447
448     /// Performs lint on `digits` (no decimal point) and returns the group
449     /// size on success or `WarningType` when emitting a warning.
450     fn do_lint(digits: &str, suffix: Option<&str>) -> Result<usize, WarningType> {
451         if let Some(suffix) = suffix {
452             if is_mistyped_suffix(suffix) {
453                 return Err(WarningType::MistypedLiteralSuffix);
454             }
455         }
456         // Grab underscore indices with respect to the units digit.
457         let underscore_positions: Vec<usize> = digits
458             .chars()
459             .rev()
460             .enumerate()
461             .filter_map(|(idx, digit)| if digit == '_' { Some(idx) } else { None })
462             .collect();
463
464         if underscore_positions.is_empty() {
465             // Check if literal needs underscores.
466             if digits.len() > 5 {
467                 Err(WarningType::UnreadableLiteral)
468             } else {
469                 Ok(0)
470             }
471         } else {
472             // Check consistency and the sizes of the groups.
473             let group_size = underscore_positions[0];
474             let consistent = underscore_positions
475                 .windows(2)
476                 .all(|ps| ps[1] - ps[0] == group_size + 1)
477                 // number of digits to the left of the last group cannot be bigger than group size.
478                 && (digits.len() - underscore_positions.last()
479                                                        .expect("there's at least one element") <= group_size + 1);
480
481             if !consistent {
482                 return Err(WarningType::InconsistentDigitGrouping);
483             } else if group_size > 4 {
484                 return Err(WarningType::LargeDigitGroups);
485             }
486             Ok(group_size)
487         }
488     }
489 }
490
491 #[derive(Copy, Clone)]
492 pub struct LiteralRepresentation {
493     threshold: u64,
494 }
495
496 impl LintPass for LiteralRepresentation {
497     fn get_lints(&self) -> LintArray {
498         lint_array!(DECIMAL_LITERAL_REPRESENTATION)
499     }
500
501     fn name(&self) -> &'static str {
502         "DecimalLiteralRepresentation"
503     }
504 }
505
506 impl EarlyLintPass for LiteralRepresentation {
507     fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
508         if in_external_macro(cx.sess(), expr.span) {
509             return;
510         }
511
512         if let ExprKind::Lit(ref lit) = expr.node {
513             self.check_lit(cx, lit)
514         }
515     }
516 }
517
518 impl LiteralRepresentation {
519     pub fn new(threshold: u64) -> Self {
520         Self { threshold }
521     }
522     fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
523         // Lint integral literals.
524         if_chain! {
525             if let LitKind::Int(..) = lit.node;
526             if let Some(src) = snippet_opt(cx, lit.span);
527             if let Some(firstch) = src.chars().next();
528             if char::to_digit(firstch, 10).is_some();
529             let digit_info = DigitInfo::new(&src, false);
530             if digit_info.radix == Radix::Decimal;
531             if let Ok(val) = digit_info.digits
532                 .chars()
533                 .filter(|&c| c != '_')
534                 .collect::<String>()
535                 .parse::<u128>();
536             if val >= u128::from(self.threshold);
537             then {
538                 let hex = format!("{:#X}", val);
539                 let digit_info = DigitInfo::new(&hex, false);
540                 let _ = Self::do_lint(digit_info.digits).map_err(|warning_type| {
541                     warning_type.display(&digit_info.grouping_hint(), cx, lit.span)
542                 });
543             }
544         }
545     }
546
547     fn do_lint(digits: &str) -> Result<(), WarningType> {
548         if digits.len() == 1 {
549             // Lint for 1 digit literals, if someone really sets the threshold that low
550             if digits == "1"
551                 || digits == "2"
552                 || digits == "4"
553                 || digits == "8"
554                 || digits == "3"
555                 || digits == "7"
556                 || digits == "F"
557             {
558                 return Err(WarningType::DecimalRepresentation);
559             }
560         } else if digits.len() < 4 {
561             // Lint for Literals with a hex-representation of 2 or 3 digits
562             let f = &digits[0..1]; // first digit
563             let s = &digits[1..]; // suffix
564
565             // Powers of 2
566             if ((f.eq("1") || f.eq("2") || f.eq("4") || f.eq("8")) && s.chars().all(|c| c == '0'))
567                 // Powers of 2 minus 1
568                 || ((f.eq("1") || f.eq("3") || f.eq("7") || f.eq("F")) && s.chars().all(|c| c == 'F'))
569             {
570                 return Err(WarningType::DecimalRepresentation);
571             }
572         } else {
573             // Lint for Literals with a hex-representation of 4 digits or more
574             let f = &digits[0..1]; // first digit
575             let m = &digits[1..digits.len() - 1]; // middle digits, except last
576             let s = &digits[1..]; // suffix
577
578             // Powers of 2 with a margin of +15/-16
579             if ((f.eq("1") || f.eq("2") || f.eq("4") || f.eq("8")) && m.chars().all(|c| c == '0'))
580                 || ((f.eq("1") || f.eq("3") || f.eq("7") || f.eq("F")) && m.chars().all(|c| c == 'F'))
581                 // Lint for representations with only 0s and Fs, while allowing 7 as the first
582                 // digit
583                 || ((f.eq("7") || f.eq("F")) && s.chars().all(|c| c == '0' || c == 'F'))
584             {
585                 return Err(WarningType::DecimalRepresentation);
586             }
587         }
588
589         Ok(())
590     }
591 }
592
593 fn is_mistyped_suffix(suffix: &str) -> bool {
594     ["_8", "_16", "_32", "_64"].contains(&suffix)
595 }
596
597 fn is_possible_suffix_index(lit: &str, idx: usize, len: usize) -> bool {
598     ((len > 3 && idx == len - 3) || (len > 2 && idx == len - 2)) && is_mistyped_suffix(lit.split_at(idx).1)
599 }
600
601 fn is_mistyped_float_suffix(suffix: &str) -> bool {
602     ["_32", "_64"].contains(&suffix)
603 }
604
605 fn is_possible_float_suffix_index(lit: &str, idx: usize, len: usize) -> bool {
606     (len > 3 && idx == len - 3) && is_mistyped_float_suffix(lit.split_at(idx).1)
607 }
608
609 fn has_possible_float_suffix(lit: &str) -> bool {
610     lit.ends_with("_32") || lit.ends_with("_64")
611 }