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