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