Skip to content

Commit 74c7880

Browse files
authored
Merge pull request #1145 from birkenfeld/issue-703-2
Lint literal suffixes not separated by underscores (idea also from #703)
2 parents 97716b3 + 2f8247a commit 74c7880

File tree

10 files changed

+124
-8
lines changed

10 files changed

+124
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ All notable changes to this project will be documented in this file.
211211
[`mem_forget`]: https://github.com/Manishearth/rust-clippy/wiki#mem_forget
212212
[`min_max`]: https://github.com/Manishearth/rust-clippy/wiki#min_max
213213
[`misrefactored_assign_op`]: https://github.com/Manishearth/rust-clippy/wiki#misrefactored_assign_op
214+
[`mixed_case_hex_literals`]: https://github.com/Manishearth/rust-clippy/wiki#mixed_case_hex_literals
214215
[`modulo_one`]: https://github.com/Manishearth/rust-clippy/wiki#modulo_one
215216
[`mut_mut`]: https://github.com/Manishearth/rust-clippy/wiki#mut_mut
216217
[`mutex_atomic`]: https://github.com/Manishearth/rust-clippy/wiki#mutex_atomic
@@ -280,6 +281,7 @@ All notable changes to this project will be documented in this file.
280281
[`unnecessary_operation`]: https://github.com/Manishearth/rust-clippy/wiki#unnecessary_operation
281282
[`unneeded_field_pattern`]: https://github.com/Manishearth/rust-clippy/wiki#unneeded_field_pattern
282283
[`unsafe_removed_from_name`]: https://github.com/Manishearth/rust-clippy/wiki#unsafe_removed_from_name
284+
[`unseparated_literal_suffix`]: https://github.com/Manishearth/rust-clippy/wiki#unseparated_literal_suffix
283285
[`unstable_as_mut_slice`]: https://github.com/Manishearth/rust-clippy/wiki#unstable_as_mut_slice
284286
[`unstable_as_slice`]: https://github.com/Manishearth/rust-clippy/wiki#unstable_as_slice
285287
[`unused_collect`]: https://github.com/Manishearth/rust-clippy/wiki#unused_collect

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Table of contents:
1717

1818
## Lints
1919

20-
There are 160 lints included in this crate:
20+
There are 162 lints included in this crate:
2121

2222
name | default | meaning
2323
---------------------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -98,6 +98,7 @@ name
9898
[mem_forget](https://github.com/Manishearth/rust-clippy/wiki#mem_forget) | allow | `mem::forget` usage on `Drop` types is likely to cause memory leaks
9999
[min_max](https://github.com/Manishearth/rust-clippy/wiki#min_max) | warn | `min(_, max(_, _))` (or vice versa) with bounds clamping the result to a constant
100100
[misrefactored_assign_op](https://github.com/Manishearth/rust-clippy/wiki#misrefactored_assign_op) | warn | having a variable on both sides of an assign op
101+
[mixed_case_hex_literals](https://github.com/Manishearth/rust-clippy/wiki#mixed_case_hex_literals) | warn | letter digits in hex literals should be either completely upper- or lowercased
101102
[modulo_one](https://github.com/Manishearth/rust-clippy/wiki#modulo_one) | warn | taking a number modulo 1, which always returns 0
102103
[mut_mut](https://github.com/Manishearth/rust-clippy/wiki#mut_mut) | allow | usage of double-mut refs, e.g. `&mut &mut ...`
103104
[mutex_atomic](https://github.com/Manishearth/rust-clippy/wiki#mutex_atomic) | warn | using a mutex where an atomic value could be used instead
@@ -165,6 +166,7 @@ name
165166
[unnecessary_operation](https://github.com/Manishearth/rust-clippy/wiki#unnecessary_operation) | warn | outer expressions with no effect
166167
[unneeded_field_pattern](https://github.com/Manishearth/rust-clippy/wiki#unneeded_field_pattern) | warn | Struct fields are bound to a wildcard instead of using `..`
167168
[unsafe_removed_from_name](https://github.com/Manishearth/rust-clippy/wiki#unsafe_removed_from_name) | warn | unsafe removed from name
169+
[unseparated_literal_suffix](https://github.com/Manishearth/rust-clippy/wiki#unseparated_literal_suffix) | allow | literal suffixes should be separated with an underscore
168170
[unused_collect](https://github.com/Manishearth/rust-clippy/wiki#unused_collect) | warn | `collect()`ing an iterator without using the result; this is usually better written as a for loop
169171
[unused_label](https://github.com/Manishearth/rust-clippy/wiki#unused_label) | warn | unused label
170172
[unused_lifetimes](https://github.com/Manishearth/rust-clippy/wiki#unused_lifetimes) | warn | unused lifetimes in function definitions

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
277277
methods::RESULT_UNWRAP_USED,
278278
methods::WRONG_PUB_SELF_CONVENTION,
279279
misc::USED_UNDERSCORE_BINDING,
280+
misc_early::UNSEPARATED_LITERAL_SUFFIX,
280281
mut_mut::MUT_MUT,
281282
mutex_atomic::MUTEX_INTEGER,
282283
non_expressive_names::SIMILAR_NAMES,
@@ -377,6 +378,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
377378
misc::TOPLEVEL_REF_ARG,
378379
misc_early::DOUBLE_NEG,
379380
misc_early::DUPLICATE_UNDERSCORE_ARGUMENT,
381+
misc_early::MIXED_CASE_HEX_LITERALS,
380382
misc_early::REDUNDANT_CLOSURE_CALL,
381383
misc_early::UNNEEDED_FIELD_PATTERN,
382384
mut_reference::UNNECESSARY_MUT_PASSED,

clippy_lints/src/misc_early.rs

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use rustc::lint::*;
22
use std::collections::HashMap;
3+
use std::char;
34
use syntax::ast::*;
45
use syntax::codemap::Span;
56
use syntax::visit::FnKind;
6-
use utils::{span_lint, span_help_and_lint, snippet, span_lint_and_then};
7+
use utils::{span_lint, span_help_and_lint, snippet, snippet_opt, span_lint_and_then};
78
/// **What it does:** This lint checks for structure field patterns bound to wildcards.
89
///
910
/// **Why is this bad?** Using `..` instead is shorter and leaves the focus on the fields that are actually bound.
@@ -64,13 +65,44 @@ declare_lint! {
6465
"`--x` is a double negation of `x` and not a pre-decrement as in C or C++"
6566
}
6667

68+
/// **What it does:** Warns on hexadecimal literals with mixed-case letter digits.
69+
///
70+
/// **Why is this bad?** It looks confusing.
71+
///
72+
/// **Known problems:** None.
73+
///
74+
/// **Example:**
75+
/// ```rust
76+
/// let y = 0x1a9BAcD;
77+
/// ```
78+
declare_lint! {
79+
pub MIXED_CASE_HEX_LITERALS, Warn,
80+
"letter digits in hex literals should be either completely upper- or lowercased"
81+
}
82+
83+
/// **What it does:** Warns if literal suffixes are not separated by an underscore.
84+
///
85+
/// **Why is this bad?** It is much less readable.
86+
///
87+
/// **Known problems:** None.
88+
///
89+
/// **Example:**
90+
/// ```rust
91+
/// let y = 123832i32;
92+
/// ```
93+
declare_lint! {
94+
pub UNSEPARATED_LITERAL_SUFFIX, Allow,
95+
"literal suffixes should be separated with an underscore"
96+
}
97+
6798

6899
#[derive(Copy, Clone)]
69100
pub struct MiscEarly;
70101

71102
impl LintPass for MiscEarly {
72103
fn get_lints(&self) -> LintArray {
73-
lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, REDUNDANT_CLOSURE_CALL, DOUBLE_NEG)
104+
lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, REDUNDANT_CLOSURE_CALL,
105+
DOUBLE_NEG, MIXED_CASE_HEX_LITERALS, UNSEPARATED_LITERAL_SUFFIX)
74106
}
75107
}
76108

@@ -174,7 +206,60 @@ impl EarlyLintPass for MiscEarly {
174206
DOUBLE_NEG,
175207
expr.span,
176208
"`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op");
177-
}
209+
}
210+
}
211+
ExprKind::Lit(ref lit) => {
212+
if_let_chain! {[
213+
let LitKind::Int(..) = lit.node,
214+
let Some(src) = snippet_opt(cx, lit.span),
215+
let Some(firstch) = src.chars().next(),
216+
char::to_digit(firstch, 10).is_some()
217+
], {
218+
let mut prev = '\0';
219+
for ch in src.chars() {
220+
if ch == 'i' || ch == 'u' {
221+
if prev != '_' {
222+
span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span,
223+
"integer type suffix should be separated by an underscore");
224+
}
225+
break;
226+
}
227+
prev = ch;
228+
}
229+
if src.starts_with("0x") {
230+
let mut seen = (false, false);
231+
for ch in src.chars() {
232+
match ch {
233+
'a' ... 'f' => seen.0 = true,
234+
'A' ... 'F' => seen.1 = true,
235+
'i' | 'u' => break, // start of suffix already
236+
_ => ()
237+
}
238+
}
239+
if seen.0 && seen.1 {
240+
span_lint(cx, MIXED_CASE_HEX_LITERALS, lit.span,
241+
"inconsistent casing in hexadecimal literal");
242+
}
243+
}
244+
}}
245+
if_let_chain! {[
246+
let LitKind::Float(..) = lit.node,
247+
let Some(src) = snippet_opt(cx, lit.span),
248+
let Some(firstch) = src.chars().next(),
249+
char::to_digit(firstch, 10).is_some()
250+
], {
251+
let mut prev = '\0';
252+
for ch in src.chars() {
253+
if ch == 'f' {
254+
if prev != '_' {
255+
span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span,
256+
"float type suffix should be separated by an underscore");
257+
}
258+
break;
259+
}
260+
prev = ch;
261+
}
262+
}}
178263
}
179264
_ => ()
180265
}

tests/compile-fail/doc.rs

100755100644
File mode changed.

tests/compile-fail/entry.rs

100755100644
File mode changed.

tests/compile-fail/filter_methods.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ fn main() {
88
.map(|x| x * 2)
99
.collect();
1010

11-
let _: Vec<_> = vec![5i8; 6].into_iter() //~ERROR called `filter(p).flat_map(q)` on an `Iterator`
11+
let _: Vec<_> = vec![5_i8; 6].into_iter() //~ERROR called `filter(p).flat_map(q)` on an `Iterator`
1212
.filter(|&x| x == 0)
1313
.flat_map(|x| x.checked_mul(2))
1414
.collect();
1515

16-
let _: Vec<_> = vec![5i8; 6].into_iter() //~ERROR called `filter_map(p).flat_map(q)` on an `Iterator`
16+
let _: Vec<_> = vec![5_i8; 6].into_iter() //~ERROR called `filter_map(p).flat_map(q)` on an `Iterator`
1717
.filter_map(|x| x.checked_mul(2))
1818
.flat_map(|x| x.checked_mul(2))
1919
.collect();
2020

21-
let _: Vec<_> = vec![5i8; 6].into_iter() //~ERROR called `filter_map(p).map(q)` on an `Iterator`
21+
let _: Vec<_> = vec![5_i8; 6].into_iter() //~ERROR called `filter_map(p).map(q)` on an `Iterator`
2222
.filter_map(|x| x.checked_mul(2))
2323
.map(|x| x.checked_mul(2))
2424
.collect();

tests/compile-fail/if_not_else.rs

100755100644
File mode changed.

tests/compile-fail/literals.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#![feature(plugin)]
2+
#![plugin(clippy)]
3+
#![deny(mixed_case_hex_literals)]
4+
#![deny(unseparated_literal_suffix)]
5+
#![allow(dead_code)]
6+
7+
fn main() {
8+
let ok1 = 0xABCD;
9+
let ok3 = 0xab_cd;
10+
let ok4 = 0xab_cd_i32;
11+
let ok5 = 0xAB_CD_u32;
12+
let ok5 = 0xAB_CD_isize;
13+
let fail1 = 0xabCD; //~ERROR inconsistent casing in hexadecimal literal
14+
let fail2 = 0xabCD_u32; //~ERROR inconsistent casing in hexadecimal literal
15+
let fail2 = 0xabCD_isize; //~ERROR inconsistent casing in hexadecimal literal
16+
17+
let ok6 = 1234_i32;
18+
let ok7 = 1234_f32;
19+
let ok8 = 1234_isize;
20+
let fail3 = 1234i32; //~ERROR integer type suffix should be separated
21+
let fail4 = 1234u32; //~ERROR integer type suffix should be separated
22+
let fail5 = 1234isize; //~ERROR integer type suffix should be separated
23+
let fail6 = 1234usize; //~ERROR integer type suffix should be separated
24+
let fail7 = 1.5f32; //~ERROR float type suffix should be separated
25+
}

tests/compile-fail/shadow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn main() {
2020
let y = 1;
2121
let x = y; //~ERROR `x` is shadowed by `y`
2222

23-
let o = Some(1u8);
23+
let o = Some(1_u8);
2424

2525
if let Some(p) = o { assert_eq!(1, p); }
2626
match o {

0 commit comments

Comments
 (0)