]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/literal_representation.rs
Fix #2494 add suggestion for unreadable_literal
[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' {
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         // Lint integral literals.
289         if_chain! {
290             if let LitKind::Int(..) = lit.node;
291             if let Some(src) = snippet_opt(cx, lit.span);
292             if let Some(firstch) = src.chars().next();
293             if char::to_digit(firstch, 10).is_some();
294             then {
295                 let digit_info = DigitInfo::new(&src, false);
296                 let _ = Self::do_lint(digit_info.digits).map_err(|warning_type| {
297                     warning_type.display(&digit_info.grouping_hint(), cx, &lit.span)
298                 });
299             }
300         }
301
302         // Lint floating-point literals.
303         if_chain! {
304             if let LitKind::Float(..) = lit.node;
305             if let Some(src) = snippet_opt(cx, lit.span);
306             if let Some(firstch) = src.chars().next();
307             if char::to_digit(firstch, 10).is_some();
308             then {
309                 let digit_info = DigitInfo::new(&src, true);
310                 // Separate digits into integral and fractional parts.
311                 let parts: Vec<&str> = digit_info
312                     .digits
313                     .split_terminator('.')
314                     .collect();
315
316                 // Lint integral and fractional parts separately, and then check consistency of digit
317                 // groups if both pass.
318                 let _ = Self::do_lint(parts[0])
319                     .map(|integral_group_size| {
320                         if parts.len() > 1 {
321                             // Lint the fractional part of literal just like integral part, but reversed.
322                             let fractional_part = &parts[1].chars().rev().collect::<String>();
323                             let _ = Self::do_lint(fractional_part)
324                                 .map(|fractional_group_size| {
325                                     let consistent = Self::parts_consistent(integral_group_size,
326                                                                             fractional_group_size,
327                                                                             parts[0].len(),
328                                                                             parts[1].len());
329                                     if !consistent {
330                                         WarningType::InconsistentDigitGrouping.display(&digit_info.grouping_hint(),
331                                                                                        cx,
332                                                                                        &lit.span);
333                                     }
334                                 })
335                                 .map_err(|warning_type| warning_type.display(&digit_info.grouping_hint(),
336                                                                              cx,
337                                                                              &lit.span));
338                         }
339                     })
340                     .map_err(|warning_type| warning_type.display(&digit_info.grouping_hint(), cx, &lit.span));
341             }
342         }
343     }
344
345     /// Given the sizes of the digit groups of both integral and fractional
346     /// parts, and the length
347     /// of both parts, determine if the digits have been grouped consistently.
348     fn parts_consistent(int_group_size: usize, frac_group_size: usize, int_size: usize, frac_size: usize) -> bool {
349         match (int_group_size, frac_group_size) {
350             // No groups on either side of decimal point - trivially consistent.
351             (0, 0) => true,
352             // Integral part has grouped digits, fractional part does not.
353             (_, 0) => frac_size <= int_group_size,
354             // Fractional part has grouped digits, integral part does not.
355             (0, _) => int_size <= frac_group_size,
356             // Both parts have grouped digits. Groups should be the same size.
357             (_, _) => int_group_size == frac_group_size,
358         }
359     }
360
361     /// Performs lint on `digits` (no decimal point) and returns the group
362     /// size on success or `WarningType` when emitting a warning.
363     fn do_lint(digits: &str) -> Result<usize, WarningType> {
364         // Grab underscore indices with respect to the units digit.
365         let underscore_positions: Vec<usize> = digits
366             .chars()
367             .rev()
368             .enumerate()
369             .filter_map(|(idx, digit)| if digit == '_' { Some(idx) } else { None })
370             .collect();
371
372         if underscore_positions.is_empty() {
373             // Check if literal needs underscores.
374             if digits.len() > 4 {
375                 Err(WarningType::UnreadableLiteral)
376             } else {
377                 Ok(0)
378             }
379         } else {
380             // Check consistency and the sizes of the groups.
381             let group_size = underscore_positions[0];
382             let consistent = underscore_positions
383                 .windows(2)
384                 .all(|ps| ps[1] - ps[0] == group_size + 1)
385                 // number of digits to the left of the last group cannot be bigger than group size.
386                 && (digits.len() - underscore_positions.last()
387                                                        .expect("there's at least one element") <= group_size + 1);
388
389             if !consistent {
390                 return Err(WarningType::InconsistentDigitGrouping);
391             } else if group_size > 4 {
392                 return Err(WarningType::LargeDigitGroups);
393             }
394             Ok(group_size)
395         }
396     }
397 }
398
399 #[derive(Copy, Clone)]
400 pub struct LiteralRepresentation {
401     threshold: u64,
402 }
403
404 impl LintPass for LiteralRepresentation {
405     fn get_lints(&self) -> LintArray {
406         lint_array!(DECIMAL_LITERAL_REPRESENTATION)
407     }
408 }
409
410 impl EarlyLintPass for LiteralRepresentation {
411     fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
412         if in_external_macro(cx, expr.span) {
413             return;
414         }
415
416         if let ExprKind::Lit(ref lit) = expr.node {
417             self.check_lit(cx, lit)
418         }
419     }
420 }
421
422 impl LiteralRepresentation {
423     pub fn new(threshold: u64) -> Self {
424         Self {
425             threshold: threshold,
426         }
427     }
428     fn check_lit(&self, cx: &EarlyContext, lit: &Lit) {
429         // Lint integral literals.
430         if_chain! {
431             if let LitKind::Int(..) = lit.node;
432             if let Some(src) = snippet_opt(cx, lit.span);
433             if let Some(firstch) = src.chars().next();
434             if char::to_digit(firstch, 10).is_some();
435             then {
436                 let digit_info = DigitInfo::new(&src, false);
437                 if digit_info.radix == Radix::Decimal {
438                     let val = digit_info.digits
439                         .chars()
440                         .filter(|&c| c != '_')
441                         .collect::<String>()
442                         .parse::<u128>().unwrap();
443                     if val < self.threshold as u128 {
444                         return
445                     }
446                     let hex = format!("{:#X}", val);
447                     let digit_info = DigitInfo::new(&hex[..], false);
448                     let _ = Self::do_lint(digit_info.digits).map_err(|warning_type| {
449                         warning_type.display(&digit_info.grouping_hint(), cx, &lit.span)
450                     });
451                 }
452             }
453         }
454     }
455
456     fn do_lint(digits: &str) -> Result<(), WarningType> {
457         if digits.len() == 1 {
458             // Lint for 1 digit literals, if someone really sets the threshold that low
459             if digits == "1" || digits == "2" || digits == "4" || digits == "8" || digits == "3" || digits == "7"
460                 || digits == "F"
461             {
462                 return Err(WarningType::DecimalRepresentation);
463             }
464         } else if digits.len() < 4 {
465             // Lint for Literals with a hex-representation of 2 or 3 digits
466             let f = &digits[0..1]; // first digit
467             let s = &digits[1..]; // suffix
468             // Powers of 2
469             if ((f.eq("1") || f.eq("2") || f.eq("4") || f.eq("8")) && s.chars().all(|c| c == '0'))
470                 // Powers of 2 minus 1
471                 || ((f.eq("1") || f.eq("3") || f.eq("7") || f.eq("F")) && s.chars().all(|c| c == 'F'))
472             {
473                 return Err(WarningType::DecimalRepresentation);
474             }
475         } else {
476             // Lint for Literals with a hex-representation of 4 digits or more
477             let f = &digits[0..1]; // first digit
478             let m = &digits[1..digits.len() - 1]; // middle digits, except last
479             let s = &digits[1..]; // suffix
480             // Powers of 2 with a margin of +15/-16
481             if ((f.eq("1") || f.eq("2") || f.eq("4") || f.eq("8")) && m.chars().all(|c| c == '0'))
482                 || ((f.eq("1") || f.eq("3") || f.eq("7") || f.eq("F")) && m.chars().all(|c| c == 'F'))
483                 // Lint for representations with only 0s and Fs, while allowing 7 as the first
484                 // digit
485                 || ((f.eq("7") || f.eq("F")) && s.chars().all(|c| c == '0' || c == 'F'))
486             {
487                 return Err(WarningType::DecimalRepresentation);
488             }
489         }
490
491         Ok(())
492     }
493 }