-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lint literal suffixes not separated by underscores (idea also from #703) #1145
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
use rustc::lint::*; | ||
use std::collections::HashMap; | ||
use std::char; | ||
use syntax::ast::*; | ||
use syntax::codemap::Span; | ||
use syntax::visit::FnKind; | ||
use utils::{span_lint, span_help_and_lint, snippet, span_lint_and_then}; | ||
use utils::{span_lint, span_help_and_lint, snippet, snippet_opt, span_lint_and_then}; | ||
/// **What it does:** This lint checks for structure field patterns bound to wildcards. | ||
/// | ||
/// **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! { | |
"`--x` is a double negation of `x` and not a pre-decrement as in C or C++" | ||
} | ||
|
||
/// **What it does:** Warns on hexadecimal literals with mixed-case letter digits. | ||
/// | ||
/// **Why is this bad?** It looks confusing. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// let y = 0x1a9BAcD; | ||
/// ``` | ||
declare_lint! { | ||
pub MIXED_CASE_HEX_LITERALS, Warn, | ||
"letter digits in hex literals should be either completely upper- or lowercased" | ||
} | ||
|
||
/// **What it does:** Warns if literal suffixes are not separated by an underscore. | ||
/// | ||
/// **Why is this bad?** It is much less readable. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// let y = 123832i32; | ||
/// ``` | ||
declare_lint! { | ||
pub UNSEPARATED_LITERAL_SUFFIX, Allow, | ||
"literal suffixes should be separated with an underscore" | ||
} | ||
|
||
|
||
#[derive(Copy, Clone)] | ||
pub struct MiscEarly; | ||
|
||
impl LintPass for MiscEarly { | ||
fn get_lints(&self) -> LintArray { | ||
lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, REDUNDANT_CLOSURE_CALL, DOUBLE_NEG) | ||
lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, REDUNDANT_CLOSURE_CALL, | ||
DOUBLE_NEG, MIXED_CASE_HEX_LITERALS, UNSEPARATED_LITERAL_SUFFIX) | ||
} | ||
} | ||
|
||
|
@@ -174,7 +206,60 @@ impl EarlyLintPass for MiscEarly { | |
DOUBLE_NEG, | ||
expr.span, | ||
"`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op"); | ||
} | ||
} | ||
} | ||
ExprKind::Lit(ref lit) => { | ||
if_let_chain! {[ | ||
let LitKind::Int(..) = lit.node, | ||
let Some(src) = snippet_opt(cx, lit.span), | ||
let Some(firstch) = src.chars().next(), | ||
char::to_digit(firstch, 10).is_some() | ||
], { | ||
let mut prev = '\0'; | ||
for ch in src.chars() { | ||
if ch == 'i' || ch == 'u' { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also f? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Er, never mind, you handle those separately. Not yet fully awake I guess :p |
||
if prev != '_' { | ||
span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span, | ||
"integer type suffix should be separated by an underscore"); | ||
} | ||
break; | ||
} | ||
prev = ch; | ||
} | ||
if src.starts_with("0x") { | ||
let mut seen = (false, false); | ||
for ch in src.chars() { | ||
match ch { | ||
'a' ... 'f' => seen.0 = true, | ||
'A' ... 'F' => seen.1 = true, | ||
'i' | 'u' => break, // start of suffix already | ||
_ => () | ||
} | ||
} | ||
if seen.0 && seen.1 { | ||
span_lint(cx, MIXED_CASE_HEX_LITERALS, lit.span, | ||
"inconsistent casing in hexadecimal literal"); | ||
} | ||
} | ||
}} | ||
if_let_chain! {[ | ||
let LitKind::Float(..) = lit.node, | ||
let Some(src) = snippet_opt(cx, lit.span), | ||
let Some(firstch) = src.chars().next(), | ||
char::to_digit(firstch, 10).is_some() | ||
], { | ||
let mut prev = '\0'; | ||
for ch in src.chars() { | ||
if ch == 'f' { | ||
if prev != '_' { | ||
span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span, | ||
"float type suffix should be separated by an underscore"); | ||
} | ||
break; | ||
} | ||
prev = ch; | ||
} | ||
}} | ||
} | ||
_ => () | ||
} | ||
|
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#![feature(plugin)] | ||
#![plugin(clippy)] | ||
#![deny(mixed_case_hex_literals)] | ||
#![deny(unseparated_literal_suffix)] | ||
#![allow(dead_code)] | ||
|
||
fn main() { | ||
let ok1 = 0xABCD; | ||
let ok3 = 0xab_cd; | ||
let ok4 = 0xab_cd_i32; | ||
let ok5 = 0xAB_CD_u32; | ||
let ok5 = 0xAB_CD_isize; | ||
let fail1 = 0xabCD; //~ERROR inconsistent casing in hexadecimal literal | ||
let fail2 = 0xabCD_u32; //~ERROR inconsistent casing in hexadecimal literal | ||
let fail2 = 0xabCD_isize; //~ERROR inconsistent casing in hexadecimal literal | ||
|
||
let ok6 = 1234_i32; | ||
let ok7 = 1234_f32; | ||
let ok8 = 1234_isize; | ||
let fail3 = 1234i32; //~ERROR integer type suffix should be separated | ||
let fail4 = 1234u32; //~ERROR integer type suffix should be separated | ||
let fail5 = 1234isize; //~ERROR integer type suffix should be separated | ||
let fail6 = 1234usize; //~ERROR integer type suffix should be separated | ||
let fail7 = 1.5f32; //~ERROR float type suffix should be separated | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this because of NaN and Infinity? If so, leave a comment :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, never mind, macros