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