2020-07-18 22:33:54 +00:00
|
|
|
use crate::utils::{constants, snippet_opt, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
|
2020-03-01 03:23:33 +00:00
|
|
|
use rustc_ast::ast::{
|
2020-07-18 22:33:54 +00:00
|
|
|
BindingMode, Expr, ExprKind, GenericParamKind, Generics, Lit, LitFloatType, LitIntType, LitKind, Mutability,
|
|
|
|
NodeId, Pat, PatKind, UnOp,
|
2020-03-01 03:23:33 +00:00
|
|
|
};
|
2020-07-18 22:33:54 +00:00
|
|
|
use rustc_ast::visit::FnKind;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
|
|
|
use rustc_errors::Applicability;
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-04 10:00:00 +00:00
|
|
|
use rustc_span::source_map::Span;
|
2016-08-06 07:55:04 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** 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.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2020-04-28 09:08:38 +00:00
|
|
|
/// ```rust
|
|
|
|
/// # struct Foo {
|
|
|
|
/// # a: i32,
|
|
|
|
/// # b: i32,
|
|
|
|
/// # c: i32,
|
|
|
|
/// # }
|
|
|
|
/// let f = Foo { a: 0, b: 0, c: 0 };
|
|
|
|
///
|
|
|
|
/// // Bad
|
|
|
|
/// match f {
|
|
|
|
/// Foo { a: _, b: 0, .. } => {},
|
|
|
|
/// Foo { a: _, b: _, c: _ } => {},
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// match f {
|
|
|
|
/// Foo { b: 0, .. } => {},
|
|
|
|
/// Foo { .. } => {},
|
|
|
|
/// }
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2016-08-06 08:18:36 +00:00
|
|
|
pub UNNEEDED_FIELD_PATTERN,
|
2020-02-19 23:32:59 +00:00
|
|
|
restriction,
|
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
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for function arguments having the similar names
|
|
|
|
/// differing by an underscore.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** It affects code readability.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2020-05-31 09:38:48 +00:00
|
|
|
/// // Bad
|
2019-03-05 16:50:33 +00:00
|
|
|
/// fn foo(a: i32, _a: i32) {}
|
2020-05-31 09:38:48 +00:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// fn bar(a: i32, _b: i32) {}
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
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
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Detects expressions of the form `--x`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** It can mislead C/C++ programmers to think `x` was
|
|
|
|
/// decremented.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2019-03-05 22:23:50 +00:00
|
|
|
/// let mut x = 3;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// --x;
|
|
|
|
/// ```
|
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
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Warns on hexadecimal literals with mixed-case letter
|
|
|
|
/// digits.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** It looks confusing.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2020-05-31 09:38:48 +00:00
|
|
|
/// // Bad
|
2019-03-05 16:50:33 +00:00
|
|
|
/// let y = 0x1a9BAcD;
|
2020-05-31 09:38:48 +00:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// let y = 0x1A9BACD;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
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
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **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
|
2020-05-31 09:38:48 +00:00
|
|
|
/// // Bad
|
2019-03-05 16:50:33 +00:00
|
|
|
/// let y = 123832i32;
|
2020-05-31 09:38:48 +00:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// let y = 123832_i32;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
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
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Warns if an integral constant literal starts with `0`.
|
|
|
|
///
|
|
|
|
/// **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
|
|
|
|
/// 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);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// prints `83` (as `83 == 0o123` while `123 == 0o173`).
|
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`"
|
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +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:**
|
|
|
|
///
|
2019-03-05 22:23:50 +00:00
|
|
|
/// ```ignore
|
2019-03-05 16:50:33 +00:00
|
|
|
/// impl<u32> Foo<u32> {
|
|
|
|
/// fn impl_func(&self) -> u32 {
|
|
|
|
/// 42
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
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"
|
|
|
|
}
|
|
|
|
|
2019-09-02 19:49:14 +00:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for patterns in the form `name @ _`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** It's almost always more readable to just use direct
|
|
|
|
/// bindings.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// # let v = Some("abc");
|
|
|
|
///
|
2020-05-31 09:38:48 +00:00
|
|
|
/// // Bad
|
|
|
|
/// match v {
|
|
|
|
/// Some(x) => (),
|
|
|
|
/// y @ _ => (),
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Good
|
2019-09-02 19:49:14 +00:00
|
|
|
/// match v {
|
|
|
|
/// Some(x) => (),
|
2020-05-31 09:38:48 +00:00
|
|
|
/// y => (),
|
2019-09-02 19:49:14 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub REDUNDANT_PATTERN,
|
|
|
|
style,
|
|
|
|
"using `name @ _` in a pattern"
|
|
|
|
}
|
|
|
|
|
2019-09-12 06:25:05 +00:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for tuple patterns with a wildcard
|
2019-09-13 06:20:27 +00:00
|
|
|
/// pattern (`_`) is next to a rest pattern (`..`).
|
2019-09-12 06:25:05 +00:00
|
|
|
///
|
2019-09-18 04:48:47 +00:00
|
|
|
/// _NOTE_: While `_, ..` means there is at least one element left, `..`
|
|
|
|
/// means there are 0 or more elements left. This can make a difference
|
|
|
|
/// when refactoring, but shouldn't result in errors in the refactored code,
|
|
|
|
/// since the wildcard pattern isn't used anyway.
|
2019-09-12 06:25:05 +00:00
|
|
|
/// **Why is this bad?** The wildcard pattern is unneeded as the rest pattern
|
|
|
|
/// can match that element as well.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// # struct TupleStruct(u32, u32, u32);
|
|
|
|
/// # let t = TupleStruct(1, 2, 3);
|
|
|
|
///
|
2020-05-31 09:38:48 +00:00
|
|
|
/// // Bad
|
2019-09-12 06:25:05 +00:00
|
|
|
/// match t {
|
|
|
|
/// TupleStruct(0, .., _) => (),
|
|
|
|
/// _ => (),
|
|
|
|
/// }
|
|
|
|
///
|
2020-05-31 09:38:48 +00:00
|
|
|
/// // Good
|
2019-09-12 06:25:05 +00:00
|
|
|
/// match t {
|
|
|
|
/// TupleStruct(0, ..) => (),
|
|
|
|
/// _ => (),
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub UNNEEDED_WILDCARD_PATTERN,
|
|
|
|
complexity,
|
2019-09-13 06:20:27 +00:00
|
|
|
"tuple patterns with a wildcard pattern (`_`) is next to a rest pattern (`..`)"
|
2019-09-12 06:25:05 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(MiscEarlyLints => [
|
|
|
|
UNNEEDED_FIELD_PATTERN,
|
|
|
|
DUPLICATE_UNDERSCORE_ARGUMENT,
|
|
|
|
DOUBLE_NEG,
|
|
|
|
MIXED_CASE_HEX_LITERALS,
|
|
|
|
UNSEPARATED_LITERAL_SUFFIX,
|
|
|
|
ZERO_PREFIXED_LITERAL,
|
2019-09-02 19:49:14 +00:00
|
|
|
BUILTIN_TYPE_SHADOW,
|
2019-09-12 06:25:05 +00:00
|
|
|
REDUNDANT_PATTERN,
|
|
|
|
UNNEEDED_WILDCARD_PATTERN,
|
2019-04-08 20:43:55 +00:00
|
|
|
]);
|
2015-12-06 02:21:34 +00:00
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
impl EarlyLintPass for MiscEarlyLints {
|
2018-07-23 11:01:12 +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,
|
2020-07-23 13:16:10 +00:00
|
|
|
&format!("this generic shadows the built-in type `{}`", name),
|
2017-12-22 04:42:47 +00:00
|
|
|
);
|
|
|
|
}
|
2016-08-27 23:52:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:25:45 +00:00
|
|
|
fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let PatKind::Struct(ref npat, ref pfields, _) = pat.kind {
|
2015-12-06 02:21:34 +00:00
|
|
|
let mut wilds = 0;
|
2018-11-27 20:14:15 +00:00
|
|
|
let type_name = npat
|
|
|
|
.segments
|
2017-08-09 07:30:56 +00:00
|
|
|
.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 {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let PatKind::Wild = field.pat.kind {
|
2015-12-06 02:21:34 +00:00
|
|
|
wilds += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !pfields.is_empty() && wilds == pfields.len() {
|
2020-01-27 01:56:22 +00:00
|
|
|
span_lint_and_help(
|
2017-08-09 07:30:56 +00:00
|
|
|
cx,
|
|
|
|
UNNEEDED_FIELD_PATTERN,
|
|
|
|
pat.span,
|
2020-07-23 21:31:33 +00:00
|
|
|
"all the struct fields are matched to a wildcard pattern, consider using `..`",
|
2020-04-18 10:28:29 +00:00
|
|
|
None,
|
2020-07-23 21:31:33 +00:00
|
|
|
&format!("try with `{} {{ .. }}` instead", type_name),
|
2017-08-09 07:30:56 +00:00
|
|
|
);
|
2015-12-06 02:21:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if wilds > 0 {
|
|
|
|
for field in pfields {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let PatKind::Wild = field.pat.kind {
|
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,
|
2020-07-23 21:31:33 +00:00
|
|
|
"you matched a field with a wildcard pattern, consider using `..` instead",
|
2017-08-09 07:30:56 +00:00
|
|
|
);
|
2016-01-02 04:52:13 +00:00
|
|
|
} else {
|
2020-03-08 01:50:33 +00:00
|
|
|
let mut normal = vec![];
|
|
|
|
|
|
|
|
for field in pfields {
|
|
|
|
match field.pat.kind {
|
|
|
|
PatKind::Wild => {},
|
|
|
|
_ => {
|
|
|
|
if let Ok(n) = cx.sess().source_map().span_to_snippet(field.span) {
|
|
|
|
normal.push(n);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-27 01:56:22 +00:00
|
|
|
span_lint_and_help(
|
2017-08-09 07:30:56 +00:00
|
|
|
cx,
|
|
|
|
UNNEEDED_FIELD_PATTERN,
|
|
|
|
field.span,
|
2020-07-23 21:31:33 +00:00
|
|
|
"you matched a field with a wildcard pattern, consider using `..` \
|
2017-09-05 09:33:04 +00:00
|
|
|
instead",
|
2020-04-18 10:28:29 +00:00
|
|
|
None,
|
2020-07-23 21:31:33 +00:00
|
|
|
&format!("try with `{} {{ {}, .. }}`", type_name, normal[..].join(", ")),
|
2017-08-09 07:30:56 +00:00
|
|
|
);
|
2016-01-02 04:52:13 +00:00
|
|
|
}
|
2015-12-06 02:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-02 19:49:14 +00:00
|
|
|
|
2020-03-08 09:00:23 +00:00
|
|
|
if let PatKind::Ident(left, ident, Some(ref right)) = pat.kind {
|
|
|
|
let left_binding = match left {
|
|
|
|
BindingMode::ByRef(Mutability::Mut) => "ref mut ",
|
|
|
|
BindingMode::ByRef(Mutability::Not) => "ref ",
|
2020-05-10 15:33:12 +00:00
|
|
|
BindingMode::ByValue(..) => "",
|
2020-03-08 09:00:23 +00:00
|
|
|
};
|
|
|
|
|
2019-09-27 15:16:06 +00:00
|
|
|
if let PatKind::Wild = right.kind {
|
2019-09-04 15:50:22 +00:00
|
|
|
span_lint_and_sugg(
|
2019-09-02 19:49:14 +00:00
|
|
|
cx,
|
|
|
|
REDUNDANT_PATTERN,
|
|
|
|
pat.span,
|
|
|
|
&format!(
|
|
|
|
"the `{} @ _` pattern can be written as just `{}`",
|
|
|
|
ident.name, ident.name,
|
|
|
|
),
|
2019-09-04 15:50:22 +00:00
|
|
|
"try",
|
2020-03-08 09:00:23 +00:00
|
|
|
format!("{}{}", left_binding, ident.name),
|
2019-09-04 15:50:22 +00:00
|
|
|
Applicability::MachineApplicable,
|
2019-09-02 19:49:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 06:25:05 +00:00
|
|
|
|
2019-09-12 06:36:05 +00:00
|
|
|
check_unneeded_wildcard_pattern(cx, pat);
|
2015-12-06 02:21:34 +00:00
|
|
|
}
|
2016-01-03 13:00:42 +00:00
|
|
|
|
2020-02-06 19:29:58 +00:00
|
|
|
fn check_fn(&mut self, cx: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: Span, _: NodeId) {
|
2018-09-11 23:34:52 +00:00
|
|
|
let mut registered_names: FxHashMap<String, Span> = FxHashMap::default();
|
2016-01-03 13:00:42 +00:00
|
|
|
|
2020-02-06 19:29:58 +00:00
|
|
|
for arg in &fn_kind.decl().inputs {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let PatKind::Ident(_, ident, None) = arg.pat.kind {
|
2018-06-28 13:46:58 +00:00
|
|
|
let arg_name = ident.to_string();
|
2016-01-03 13:00:42 +00:00
|
|
|
|
2020-09-14 04:11:35 +00:00
|
|
|
if let Some(arg_name) = arg_name.strip_prefix('_') {
|
|
|
|
if let Some(correspondence) = registered_names.get(arg_name) {
|
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",
|
2020-09-14 04:11:35 +00:00
|
|
|
arg_name
|
2017-08-09 07:30:56 +00:00
|
|
|
),
|
2019-08-01 05:09:57 +00:00
|
|
|
);
|
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
|
|
|
|
2018-07-23 11:01:12 +00:00
|
|
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
2018-07-24 06:55:38 +00:00
|
|
|
if in_external_macro(cx.sess(), expr.span) {
|
2017-05-09 15:15:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-09-27 15:16:06 +00:00
|
|
|
match expr.kind {
|
2018-11-27 20:14:15 +00:00
|
|
|
ExprKind::Unary(UnOp::Neg, ref inner) => {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Unary(UnOp::Neg, _) = inner.kind {
|
2018-11-27 20:14:15 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
DOUBLE_NEG,
|
|
|
|
expr.span,
|
|
|
|
"`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op",
|
|
|
|
);
|
|
|
|
}
|
2017-09-05 09:33:04 +00:00
|
|
|
},
|
2019-10-03 19:09:32 +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
|
|
|
}
|
|
|
|
}
|
2015-12-06 02:21:34 +00:00
|
|
|
}
|
2017-05-09 15:15:28 +00:00
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
impl MiscEarlyLints {
|
2019-10-03 19:09:32 +00:00
|
|
|
fn check_lit(cx: &EarlyContext<'_>, lit: &Lit) {
|
2019-09-06 10:00:53 +00:00
|
|
|
// We test if first character in snippet is a number, because the snippet could be an expansion
|
|
|
|
// from a built-in macro like `line!()` or a proc-macro like `#[wasm_bindgen]`.
|
|
|
|
// Note that this check also covers special case that `line!()` is eagerly expanded by compiler.
|
|
|
|
// See <https://github.com/rust-lang/rust-clippy/issues/4507> for a regression.
|
|
|
|
// FIXME: Find a better way to detect those cases.
|
2019-08-26 09:11:43 +00:00
|
|
|
let lit_snip = match snippet_opt(cx, lit.span) {
|
2019-09-06 10:00:53 +00:00
|
|
|
Some(snip) if snip.chars().next().map_or(false, |c| c.is_digit(10)) => snip,
|
2019-08-26 09:11:43 +00:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
2019-09-27 15:16:06 +00:00
|
|
|
if let LitKind::Int(value, lit_int_type) = lit.kind {
|
2019-08-26 09:11:43 +00:00
|
|
|
let suffix = match lit_int_type {
|
2019-11-07 12:27:00 +00:00
|
|
|
LitIntType::Signed(ty) => ty.name_str(),
|
|
|
|
LitIntType::Unsigned(ty) => ty.name_str(),
|
2019-08-26 09:11:43 +00:00
|
|
|
LitIntType::Unsuffixed => "",
|
|
|
|
};
|
|
|
|
|
2020-02-03 01:42:21 +00:00
|
|
|
let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) {
|
|
|
|
val
|
|
|
|
} else {
|
|
|
|
return; // It's useless so shouldn't lint.
|
|
|
|
};
|
2019-08-26 09:11:43 +00:00
|
|
|
// Do not lint when literal is unsuffixed.
|
|
|
|
if !suffix.is_empty() && lit_snip.as_bytes()[maybe_last_sep_idx] != b'_' {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
UNSEPARATED_LITERAL_SUFFIX,
|
|
|
|
lit.span,
|
|
|
|
"integer type suffix should be separated by an underscore",
|
|
|
|
"add an underscore",
|
|
|
|
format!("{}_{}", &lit_snip[..=maybe_last_sep_idx], suffix),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-13 05:39:29 +00:00
|
|
|
if lit_snip.starts_with("0x") {
|
|
|
|
if maybe_last_sep_idx <= 2 {
|
|
|
|
// It's meaningless or causes range error.
|
|
|
|
return;
|
|
|
|
}
|
2019-08-26 09:11:43 +00:00
|
|
|
let mut seen = (false, false);
|
|
|
|
for ch in lit_snip.as_bytes()[2..=maybe_last_sep_idx].iter() {
|
|
|
|
match ch {
|
|
|
|
b'a'..=b'f' => seen.0 = true,
|
|
|
|
b'A'..=b'F' => seen.1 = true,
|
|
|
|
_ => {},
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
|
|
|
if seen.0 && seen.1 {
|
2019-08-26 09:11:43 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
MIXED_CASE_HEX_LITERALS,
|
|
|
|
lit.span,
|
|
|
|
"inconsistent casing in hexadecimal literal",
|
|
|
|
);
|
|
|
|
break;
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
2019-08-26 09:11:43 +00:00
|
|
|
}
|
|
|
|
} else if lit_snip.starts_with("0b") || lit_snip.starts_with("0o") {
|
|
|
|
/* nothing to do */
|
|
|
|
} else if value != 0 && lit_snip.starts_with('0') {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
ZERO_PREFIXED_LITERAL,
|
|
|
|
lit.span,
|
|
|
|
"this is a decimal constant",
|
2020-04-17 06:08:00 +00:00
|
|
|
|diag| {
|
|
|
|
diag.span_suggestion(
|
2017-10-23 19:18:02 +00:00
|
|
|
lit.span,
|
2019-08-26 09:11:43 +00:00
|
|
|
"if you mean to use a decimal constant, remove the `0` to avoid confusion",
|
|
|
|
lit_snip.trim_start_matches(|c| c == '_' || c == '0').to_string(),
|
2018-09-18 17:01:17 +00:00
|
|
|
Applicability::MaybeIncorrect,
|
2017-10-23 19:18:02 +00:00
|
|
|
);
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.span_suggestion(
|
2017-10-23 19:18:02 +00:00
|
|
|
lit.span,
|
|
|
|
"if you mean to use an octal constant, use `0o`",
|
2019-08-26 09:11:43 +00:00
|
|
|
format!("0o{}", lit_snip.trim_start_matches(|c| c == '_' || c == '0')),
|
2018-09-18 17:01:17 +00:00
|
|
|
Applicability::MaybeIncorrect,
|
2017-10-23 19:18:02 +00:00
|
|
|
);
|
2019-08-26 09:11:43 +00:00
|
|
|
},
|
|
|
|
);
|
2017-05-09 15:15:28 +00:00
|
|
|
}
|
2019-11-07 12:27:00 +00:00
|
|
|
} else if let LitKind::Float(_, LitFloatType::Suffixed(float_ty)) = lit.kind {
|
|
|
|
let suffix = float_ty.name_str();
|
2020-02-03 01:42:21 +00:00
|
|
|
let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) {
|
|
|
|
val
|
|
|
|
} else {
|
|
|
|
return; // It's useless so shouldn't lint.
|
|
|
|
};
|
2019-08-26 09:11:43 +00:00
|
|
|
if lit_snip.as_bytes()[maybe_last_sep_idx] != b'_' {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
UNSEPARATED_LITERAL_SUFFIX,
|
|
|
|
lit.span,
|
|
|
|
"float type suffix should be separated by an underscore",
|
|
|
|
"add an underscore",
|
|
|
|
format!("{}_{}", &lit_snip[..=maybe_last_sep_idx], suffix),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2017-05-09 15:15:28 +00:00
|
|
|
}
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
2017-05-09 15:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-12 06:36:05 +00:00
|
|
|
|
|
|
|
fn check_unneeded_wildcard_pattern(cx: &EarlyContext<'_>, pat: &Pat) {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let PatKind::TupleStruct(_, ref patterns) | PatKind::Tuple(ref patterns) = pat.kind {
|
2019-09-12 06:36:05 +00:00
|
|
|
fn span_lint(cx: &EarlyContext<'_>, span: Span, only_one: bool) {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
UNNEEDED_WILDCARD_PATTERN,
|
|
|
|
span,
|
|
|
|
if only_one {
|
|
|
|
"this pattern is unneeded as the `..` pattern can match that element"
|
|
|
|
} else {
|
|
|
|
"these patterns are unneeded as the `..` pattern can match those elements"
|
|
|
|
},
|
|
|
|
if only_one { "remove it" } else { "remove them" },
|
|
|
|
"".to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-13 06:20:27 +00:00
|
|
|
if let Some(rest_index) = patterns.iter().position(|pat| pat.is_rest()) {
|
2019-09-12 06:36:05 +00:00
|
|
|
if let Some((left_index, left_pat)) = patterns[..rest_index]
|
|
|
|
.iter()
|
|
|
|
.rev()
|
2020-07-05 20:11:19 +00:00
|
|
|
.take_while(|pat| matches!(pat.kind, PatKind::Wild))
|
2019-09-12 06:36:05 +00:00
|
|
|
.enumerate()
|
|
|
|
.last()
|
|
|
|
{
|
|
|
|
span_lint(cx, left_pat.span.until(patterns[rest_index].span), left_index == 0);
|
|
|
|
}
|
|
|
|
|
2020-07-05 20:11:19 +00:00
|
|
|
if let Some((right_index, right_pat)) = patterns[rest_index + 1..]
|
|
|
|
.iter()
|
|
|
|
.take_while(|pat| matches!(pat.kind, PatKind::Wild))
|
|
|
|
.enumerate()
|
|
|
|
.last()
|
2019-09-12 06:36:05 +00:00
|
|
|
{
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
patterns[rest_index].span.shrink_to_hi().to(right_pat.span),
|
|
|
|
right_index == 0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|