2015-12-06 02:21:34 +00:00
|
|
|
use rustc::lint::*;
|
2016-01-03 13:00:42 +00:00
|
|
|
use std::collections::HashMap;
|
2016-08-05 16:50:02 +00:00
|
|
|
use std::char;
|
2015-12-06 02:21:34 +00:00
|
|
|
use syntax::ast::*;
|
2016-01-03 13:00:42 +00:00
|
|
|
use syntax::codemap::Span;
|
|
|
|
use syntax::visit::FnKind;
|
2018-05-30 08:15:50 +00:00
|
|
|
use crate::utils::{constants, in_external_macro, snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_then};
|
2016-08-06 07:55:04 +00:00
|
|
|
|
|
|
|
/// **What it does:** Checks for structure field patterns bound to wildcards.
|
2015-12-14 21:16:56 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Why is this bad?** Using `..` instead is shorter and leaves the focus on
|
|
|
|
/// the fields that are actually bound.
|
2015-12-14 21:16:56 +00:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
2016-07-15 22:25:44 +00:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// let { a: _, b: ref b, c: _ } = ..
|
|
|
|
/// ```
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2016-08-06 08:18:36 +00:00
|
|
|
pub UNNEEDED_FIELD_PATTERN,
|
2018-03-28 13:24:26 +00:00
|
|
|
style,
|
2016-08-06 08:18:36 +00:00
|
|
|
"struct fields bound to a wildcard instead of using `..`"
|
2016-02-05 23:13:29 +00:00
|
|
|
}
|
2015-12-06 02:21:34 +00:00
|
|
|
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **What it does:** Checks for function arguments having the similar names
|
|
|
|
/// differing by an underscore.
|
2016-01-03 13:00:42 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Why is this bad?** It affects code readability.
|
2016-01-03 13:00:42 +00:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
2016-07-15 22:25:44 +00:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// fn foo(a: i32, _a: i32) {}
|
|
|
|
/// ```
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2016-08-06 08:18:36 +00:00
|
|
|
pub DUPLICATE_UNDERSCORE_ARGUMENT,
|
2018-03-28 13:24:26 +00:00
|
|
|
style,
|
2016-08-06 08:18:36 +00:00
|
|
|
"function arguments having names which only differ by an underscore"
|
2016-02-05 23:13:29 +00:00
|
|
|
}
|
2016-01-03 13:00:42 +00:00
|
|
|
|
2017-08-09 07:30:56 +00:00
|
|
|
/// **What it does:** Detects closures called in the same expression where they
|
|
|
|
/// are defined.
|
2016-03-03 19:14:49 +00:00
|
|
|
///
|
2017-08-09 07:30:56 +00:00
|
|
|
/// **Why is this bad?** It is unnecessarily adding to the expression's
|
|
|
|
/// complexity.
|
2016-03-03 19:14:49 +00:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
2016-07-15 22:25:44 +00:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// (|| 42)()
|
|
|
|
/// ```
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2016-08-06 08:18:36 +00:00
|
|
|
pub REDUNDANT_CLOSURE_CALL,
|
2018-03-29 11:41:53 +00:00
|
|
|
complexity,
|
2016-08-06 08:18:36 +00:00
|
|
|
"throwaway closures called in the expression they are defined"
|
2016-03-03 19:14:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **What it does:** Detects expressions of the form `--x`.
|
2016-06-29 23:00:25 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Why is this bad?** It can mislead C/C++ programmers to think `x` was
|
|
|
|
/// decremented.
|
2016-06-29 23:00:25 +00:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
2016-07-15 22:25:44 +00:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// --x;
|
|
|
|
/// ```
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2016-08-06 08:18:36 +00:00
|
|
|
pub DOUBLE_NEG,
|
2018-03-28 13:24:26 +00:00
|
|
|
style,
|
2016-08-06 08:18:36 +00:00
|
|
|
"`--x`, which is a double negation of `x` and not a pre-decrement as in C/C++"
|
2016-06-29 23:00:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 07:30:56 +00:00
|
|
|
/// **What it does:** Warns on hexadecimal literals with mixed-case letter
|
|
|
|
/// digits.
|
2016-08-05 16:30:07 +00:00
|
|
|
///
|
|
|
|
/// **Why is this bad?** It looks confusing.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// let y = 0x1a9BAcD;
|
|
|
|
/// ```
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2016-08-06 08:18:36 +00:00
|
|
|
pub MIXED_CASE_HEX_LITERALS,
|
2018-03-28 13:24:26 +00:00
|
|
|
style,
|
2016-08-06 08:18:36 +00:00
|
|
|
"hex literals whose letter digits are not consistently upper- or lowercased"
|
2016-08-05 16:30:07 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 07:30:56 +00:00
|
|
|
/// **What it does:** Warns if literal suffixes are not separated by an
|
|
|
|
/// underscore.
|
2016-08-05 16:50:02 +00:00
|
|
|
///
|
|
|
|
/// **Why is this bad?** It is much less readable.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// let y = 123832i32;
|
|
|
|
/// ```
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2016-08-06 08:18:36 +00:00
|
|
|
pub UNSEPARATED_LITERAL_SUFFIX,
|
2018-03-28 13:24:26 +00:00
|
|
|
pedantic,
|
2016-08-06 08:18:36 +00:00
|
|
|
"literals whose suffix is not separated by an underscore"
|
2016-08-05 16:50:02 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 12:37:09 +00:00
|
|
|
/// **What it does:** Warns if an integral constant literal starts with `0`.
|
2016-08-20 16:11:15 +00:00
|
|
|
///
|
2017-08-09 07:30:56 +00:00
|
|
|
/// **Why is this bad?** In some languages (including the infamous C language
|
|
|
|
/// and most of its
|
|
|
|
/// family), this marks an octal constant. In Rust however, this is a decimal
|
|
|
|
/// constant. This could
|
2016-08-20 16:11:15 +00:00
|
|
|
/// be confusing for both the writer and a reader of the constant.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// In Rust:
|
|
|
|
/// ```rust
|
|
|
|
/// fn main() {
|
|
|
|
/// let a = 0123;
|
|
|
|
/// println!("{}", a);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// prints `123`, while in C:
|
|
|
|
///
|
|
|
|
/// ```c
|
|
|
|
/// #include <stdio.h>
|
|
|
|
///
|
|
|
|
/// int main() {
|
|
|
|
/// int a = 0123;
|
|
|
|
/// printf("%d\n", a);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2016-08-24 12:37:09 +00:00
|
|
|
/// prints `83` (as `83 == 0o123` while `123 == 0o173`).
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2016-08-20 16:11:15 +00:00
|
|
|
pub ZERO_PREFIXED_LITERAL,
|
2018-03-28 13:24:26 +00:00
|
|
|
complexity,
|
2016-08-20 16:11:15 +00:00
|
|
|
"integer literals starting with `0`"
|
|
|
|
}
|
|
|
|
|
2016-08-27 23:52:01 +00:00
|
|
|
/// **What it does:** Warns if a generic shadows a built-in type.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** This gives surprising type errors.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// impl<u32> Foo<u32> {
|
|
|
|
/// fn impl_func(&self) -> u32 {
|
|
|
|
/// 42
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2016-08-27 23:52:01 +00:00
|
|
|
pub BUILTIN_TYPE_SHADOW,
|
2018-03-28 13:24:26 +00:00
|
|
|
style,
|
2016-08-27 23:52:01 +00:00
|
|
|
"shadowing a builtin type"
|
|
|
|
}
|
|
|
|
|
2015-12-06 02:21:34 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct MiscEarly;
|
|
|
|
|
|
|
|
impl LintPass for MiscEarly {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
2017-08-09 07:30:56 +00:00
|
|
|
lint_array!(
|
|
|
|
UNNEEDED_FIELD_PATTERN,
|
|
|
|
DUPLICATE_UNDERSCORE_ARGUMENT,
|
|
|
|
REDUNDANT_CLOSURE_CALL,
|
|
|
|
DOUBLE_NEG,
|
|
|
|
MIXED_CASE_HEX_LITERALS,
|
|
|
|
UNSEPARATED_LITERAL_SUFFIX,
|
|
|
|
ZERO_PREFIXED_LITERAL,
|
|
|
|
BUILTIN_TYPE_SHADOW
|
|
|
|
)
|
2015-12-06 02:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for MiscEarly {
|
2016-08-27 23:52:01 +00:00
|
|
|
fn check_generics(&mut self, cx: &EarlyContext, gen: &Generics) {
|
2017-12-22 04:42:47 +00:00
|
|
|
for param in &gen.params {
|
2018-06-24 13:32:40 +00:00
|
|
|
if let GenericParamKind::Type { .. } = param.kind {
|
2018-06-28 13:46:58 +00:00
|
|
|
let name = param.ident.as_str();
|
2017-12-22 04:42:47 +00:00
|
|
|
if constants::BUILTIN_TYPES.contains(&&*name) {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
BUILTIN_TYPE_SHADOW,
|
2018-06-24 13:32:40 +00:00
|
|
|
param.ident.span,
|
2017-12-22 04:42:47 +00:00
|
|
|
&format!("This generic shadows the built-in type `{}`", name),
|
|
|
|
);
|
|
|
|
}
|
2016-08-27 23:52:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-06 02:21:34 +00:00
|
|
|
fn check_pat(&mut self, cx: &EarlyContext, pat: &Pat) {
|
2016-02-17 12:38:44 +00:00
|
|
|
if let PatKind::Struct(ref npat, ref pfields, _) = pat.node {
|
2015-12-06 02:21:34 +00:00
|
|
|
let mut wilds = 0;
|
2017-08-09 07:30:56 +00:00
|
|
|
let type_name = npat.segments
|
|
|
|
.last()
|
|
|
|
.expect("A path must have at least one segment")
|
2018-04-07 05:22:23 +00:00
|
|
|
.ident
|
2017-08-09 07:30:56 +00:00
|
|
|
.name;
|
2015-12-06 02:21:34 +00:00
|
|
|
|
|
|
|
for field in pfields {
|
2018-07-14 22:00:27 +00:00
|
|
|
if let PatKind::Wild = field.node.pat.node {
|
2015-12-06 02:21:34 +00:00
|
|
|
wilds += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !pfields.is_empty() && wilds == pfields.len() {
|
2017-08-09 07:30:56 +00:00
|
|
|
span_help_and_lint(
|
|
|
|
cx,
|
|
|
|
UNNEEDED_FIELD_PATTERN,
|
|
|
|
pat.span,
|
|
|
|
"All the struct fields are matched to a wildcard pattern, consider using `..`.",
|
|
|
|
&format!("Try with `{} {{ .. }}` instead", type_name),
|
|
|
|
);
|
2015-12-06 02:21:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if wilds > 0 {
|
2016-01-04 04:26:12 +00:00
|
|
|
let mut normal = vec![];
|
2016-01-02 04:52:13 +00:00
|
|
|
|
|
|
|
for field in pfields {
|
2018-07-14 22:00:27 +00:00
|
|
|
match field.node.pat.node {
|
|
|
|
PatKind::Wild => {},
|
|
|
|
_ => if let Ok(n) = cx.sess().codemap().span_to_snippet(field.span) {
|
2016-01-02 04:52:13 +00:00
|
|
|
normal.push(n);
|
2018-07-14 22:00:27 +00:00
|
|
|
},
|
2016-01-02 04:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-06 02:21:34 +00:00
|
|
|
for field in pfields {
|
2018-07-14 22:00:27 +00:00
|
|
|
if let PatKind::Wild = field.node.pat.node {
|
2016-01-02 04:52:13 +00:00
|
|
|
wilds -= 1;
|
|
|
|
if wilds > 0 {
|
2017-08-09 07:30:56 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
UNNEEDED_FIELD_PATTERN,
|
|
|
|
field.span,
|
|
|
|
"You matched a field with a wildcard pattern. Consider using `..` instead",
|
|
|
|
);
|
2016-01-02 04:52:13 +00:00
|
|
|
} else {
|
2017-08-09 07:30:56 +00:00
|
|
|
span_help_and_lint(
|
|
|
|
cx,
|
|
|
|
UNNEEDED_FIELD_PATTERN,
|
|
|
|
field.span,
|
|
|
|
"You matched a field with a wildcard pattern. Consider using `..` \
|
2017-09-05 09:33:04 +00:00
|
|
|
instead",
|
2017-08-09 07:30:56 +00:00
|
|
|
&format!("Try with `{} {{ {}, .. }}`", type_name, normal[..].join(", ")),
|
|
|
|
);
|
2016-01-02 04:52:13 +00:00
|
|
|
}
|
2015-12-06 02:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-03 13:00:42 +00:00
|
|
|
|
2016-11-16 20:57:56 +00:00
|
|
|
fn check_fn(&mut self, cx: &EarlyContext, _: FnKind, decl: &FnDecl, _: Span, _: NodeId) {
|
2016-01-04 04:26:12 +00:00
|
|
|
let mut registered_names: HashMap<String, Span> = HashMap::new();
|
2016-01-03 13:00:42 +00:00
|
|
|
|
2016-08-01 14:59:14 +00:00
|
|
|
for arg in &decl.inputs {
|
2018-04-07 05:22:23 +00:00
|
|
|
if let PatKind::Ident(_, ident, None) = arg.pat.node {
|
2018-06-28 13:46:58 +00:00
|
|
|
let arg_name = ident.to_string();
|
2016-01-03 13:00:42 +00:00
|
|
|
|
2016-02-15 03:40:43 +00:00
|
|
|
if arg_name.starts_with('_') {
|
2016-03-19 14:06:56 +00:00
|
|
|
if let Some(correspondence) = registered_names.get(&arg_name[1..]) {
|
2017-08-09 07:30:56 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
DUPLICATE_UNDERSCORE_ARGUMENT,
|
|
|
|
*correspondence,
|
|
|
|
&format!(
|
|
|
|
"`{}` already exists, having another argument having almost the same \
|
2017-09-05 09:33:04 +00:00
|
|
|
name makes code comprehension and documentation more difficult",
|
2017-08-09 07:30:56 +00:00
|
|
|
arg_name[1..].to_owned()
|
|
|
|
),
|
|
|
|
);;
|
2016-01-03 15:55:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-02-02 21:35:01 +00:00
|
|
|
registered_names.insert(arg_name, arg.pat.span);
|
2016-01-03 13:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-03 19:14:49 +00:00
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
|
2017-05-09 15:15:13 +00:00
|
|
|
if in_external_macro(cx, expr.span) {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-29 23:00:25 +00:00
|
|
|
match expr.node {
|
2017-09-05 09:33:04 +00:00
|
|
|
ExprKind::Call(ref paren, _) => if let ExprKind::Paren(ref closure) = paren.node {
|
2018-06-24 13:32:40 +00:00
|
|
|
if let ExprKind::Closure(_, _, _, ref decl, ref block, _) = closure.node {
|
2017-09-05 09:33:04 +00:00
|
|
|
span_lint_and_then(
|
2017-08-09 07:30:56 +00:00
|
|
|
cx,
|
2017-09-05 09:33:04 +00:00
|
|
|
REDUNDANT_CLOSURE_CALL,
|
2017-08-09 07:30:56 +00:00
|
|
|
expr.span,
|
2017-09-05 09:33:04 +00:00
|
|
|
"Try not to call a closure in the expression where it is declared.",
|
|
|
|
|db| if decl.inputs.is_empty() {
|
|
|
|
let hint = snippet(cx, block.span, "..").into_owned();
|
|
|
|
db.span_suggestion(expr.span, "Try doing something like: ", hint);
|
|
|
|
},
|
2017-08-09 07:30:56 +00:00
|
|
|
);
|
2016-08-05 16:30:07 +00:00
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2017-09-05 09:33:04 +00:00
|
|
|
ExprKind::Unary(UnOp::Neg, ref inner) => if let ExprKind::Unary(UnOp::Neg, _) = inner.node {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
DOUBLE_NEG,
|
|
|
|
expr.span,
|
|
|
|
"`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op",
|
|
|
|
);
|
|
|
|
},
|
2017-05-09 15:15:28 +00:00
|
|
|
ExprKind::Lit(ref lit) => self.check_lit(cx, lit),
|
2016-12-20 17:21:30 +00:00
|
|
|
_ => (),
|
2016-03-03 19:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_block(&mut self, cx: &EarlyContext, block: &Block) {
|
|
|
|
for w in block.stmts.windows(2) {
|
2017-10-23 19:18:02 +00:00
|
|
|
if_chain! {
|
|
|
|
if let StmtKind::Local(ref local) = w[0].node;
|
|
|
|
if let Option::Some(ref t) = local.init;
|
2018-06-24 13:32:40 +00:00
|
|
|
if let ExprKind::Closure(..) = t.node;
|
2018-04-07 05:22:23 +00:00
|
|
|
if let PatKind::Ident(_, ident, _) = local.pat.node;
|
2017-10-23 19:18:02 +00:00
|
|
|
if let StmtKind::Semi(ref second) = w[1].node;
|
|
|
|
if let ExprKind::Assign(_, ref call) = second.node;
|
|
|
|
if let ExprKind::Call(ref closure, _) = call.node;
|
|
|
|
if let ExprKind::Path(_, ref path) = closure.node;
|
|
|
|
then {
|
2018-05-29 12:45:10 +00:00
|
|
|
if ident == path.segments[0].ident {
|
2017-10-23 19:18:02 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
REDUNDANT_CLOSURE_CALL,
|
|
|
|
second.span,
|
|
|
|
"Closure called just once immediately after it was declared",
|
|
|
|
);
|
|
|
|
}
|
2016-03-03 19:14:49 +00:00
|
|
|
}
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
2016-03-03 19:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-06 02:21:34 +00:00
|
|
|
}
|
2017-05-09 15:15:28 +00:00
|
|
|
|
|
|
|
impl MiscEarly {
|
2018-05-31 18:15:48 +00:00
|
|
|
fn check_lit(self, cx: &EarlyContext, lit: &Lit) {
|
2017-10-23 19:18:02 +00:00
|
|
|
if_chain! {
|
|
|
|
if let LitKind::Int(value, ..) = lit.node;
|
|
|
|
if let Some(src) = snippet_opt(cx, lit.span);
|
|
|
|
if let Some(firstch) = src.chars().next();
|
|
|
|
if char::to_digit(firstch, 10).is_some();
|
|
|
|
then {
|
|
|
|
let mut prev = '\0';
|
2017-05-09 15:15:28 +00:00
|
|
|
for ch in src.chars() {
|
2017-10-23 19:18:02 +00:00
|
|
|
if ch == 'i' || ch == 'u' {
|
|
|
|
if prev != '_' {
|
|
|
|
span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span,
|
|
|
|
"integer type suffix should be separated by an underscore");
|
|
|
|
}
|
|
|
|
break;
|
2017-05-09 15:15:28 +00:00
|
|
|
}
|
2017-10-23 19:18:02 +00:00
|
|
|
prev = ch;
|
2017-05-09 15:15:28 +00:00
|
|
|
}
|
2017-10-23 19:18:02 +00:00
|
|
|
if src.starts_with("0x") {
|
|
|
|
let mut seen = (false, false);
|
|
|
|
for ch in src.chars() {
|
|
|
|
match ch {
|
2018-06-28 11:33:11 +00:00
|
|
|
'a' ..= 'f' => seen.0 = true,
|
|
|
|
'A' ..= 'F' => seen.1 = true,
|
2017-10-23 19:18:02 +00:00
|
|
|
'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");
|
|
|
|
}
|
|
|
|
} else if src.starts_with("0b") || src.starts_with("0o") {
|
|
|
|
/* nothing to do */
|
|
|
|
} else if value != 0 && src.starts_with('0') {
|
|
|
|
span_lint_and_then(cx,
|
|
|
|
ZERO_PREFIXED_LITERAL,
|
|
|
|
lit.span,
|
|
|
|
"this is a decimal constant",
|
|
|
|
|db| {
|
|
|
|
db.span_suggestion(
|
|
|
|
lit.span,
|
|
|
|
"if you mean to use a decimal constant, remove the `0` to remove confusion",
|
|
|
|
src.trim_left_matches(|c| c == '_' || c == '0').to_string(),
|
|
|
|
);
|
|
|
|
db.span_suggestion(
|
|
|
|
lit.span,
|
|
|
|
"if you mean to use an octal constant, use `0o`",
|
|
|
|
format!("0o{}", src.trim_left_matches(|c| c == '_' || c == '0')),
|
|
|
|
);
|
|
|
|
});
|
2017-05-09 15:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
|
|
|
if_chain! {
|
|
|
|
if let LitKind::Float(..) = lit.node;
|
|
|
|
if let Some(src) = snippet_opt(cx, lit.span);
|
|
|
|
if let Some(firstch) = src.chars().next();
|
|
|
|
if char::to_digit(firstch, 10).is_some();
|
|
|
|
then {
|
|
|
|
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;
|
2017-05-09 15:15:28 +00:00
|
|
|
}
|
2017-10-23 19:18:02 +00:00
|
|
|
prev = ch;
|
2017-05-09 15:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
2017-05-09 15:15:28 +00:00
|
|
|
}
|
|
|
|
}
|