mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-26 06:30:32 +00:00
Deprecate mem_discriminant_non_enum
This lint has been uplifted and is now included in enum_intrinsics_non_enums.
This commit is contained in:
parent
5cf4984872
commit
f44a904a56
14 changed files with 15 additions and 314 deletions
|
@ -1873,10 +1873,10 @@ Released 2019-01-17
|
|||
|
||||
[2e26fdc2...b2601be](https://github.com/rust-lang/rust-clippy/compare/2e26fdc2...b2601be)
|
||||
|
||||
* New lints: [`slow_vector_initialization`], [`mem_discriminant_non_enum`],
|
||||
* New lints: [`slow_vector_initialization`], `mem_discriminant_non_enum`,
|
||||
[`redundant_clone`], [`wildcard_dependencies`],
|
||||
[`into_iter_on_ref`], `into_iter_on_array`, [`deprecated_cfg_attr`],
|
||||
[`mem_discriminant_non_enum`], [`cargo_common_metadata`]
|
||||
[`cargo_common_metadata`]
|
||||
* Add support for `u128` and `i128` to integer related lints
|
||||
* Add float support to `mistyped_literal_suffixes`
|
||||
* Fix false positives in `use_self`
|
||||
|
@ -2839,7 +2839,6 @@ Released 2018-09-13
|
|||
[`match_wild_err_arm`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_wild_err_arm
|
||||
[`match_wildcard_for_single_variants`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_wildcard_for_single_variants
|
||||
[`maybe_infinite_iter`]: https://rust-lang.github.io/rust-clippy/master/index.html#maybe_infinite_iter
|
||||
[`mem_discriminant_non_enum`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_discriminant_non_enum
|
||||
[`mem_forget`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_forget
|
||||
[`mem_replace_option_with_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_option_with_none
|
||||
[`mem_replace_with_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_with_default
|
||||
|
|
|
@ -127,7 +127,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
|
|||
LintId::of(matches::REDUNDANT_PATTERN_MATCHING),
|
||||
LintId::of(matches::SINGLE_MATCH),
|
||||
LintId::of(matches::WILDCARD_IN_OR_PATTERNS),
|
||||
LintId::of(mem_discriminant::MEM_DISCRIMINANT_NON_ENUM),
|
||||
LintId::of(mem_replace::MEM_REPLACE_OPTION_WITH_NONE),
|
||||
LintId::of(mem_replace::MEM_REPLACE_WITH_DEFAULT),
|
||||
LintId::of(mem_replace::MEM_REPLACE_WITH_UNINIT),
|
||||
|
|
|
@ -36,7 +36,6 @@ store.register_group(true, "clippy::correctness", Some("clippy_correctness"), ve
|
|||
LintId::of(loops::ITER_NEXT_LOOP),
|
||||
LintId::of(loops::NEVER_LOOP),
|
||||
LintId::of(loops::WHILE_IMMUTABLE_CONDITION),
|
||||
LintId::of(mem_discriminant::MEM_DISCRIMINANT_NON_ENUM),
|
||||
LintId::of(mem_replace::MEM_REPLACE_WITH_UNINIT),
|
||||
LintId::of(methods::CLONE_DOUBLE_REF),
|
||||
LintId::of(methods::ITERATOR_STEP_BY_ZERO),
|
||||
|
|
|
@ -241,7 +241,6 @@ store.register_lints(&[
|
|||
matches::SINGLE_MATCH_ELSE,
|
||||
matches::WILDCARD_ENUM_MATCH_ARM,
|
||||
matches::WILDCARD_IN_OR_PATTERNS,
|
||||
mem_discriminant::MEM_DISCRIMINANT_NON_ENUM,
|
||||
mem_forget::MEM_FORGET,
|
||||
mem_replace::MEM_REPLACE_OPTION_WITH_NONE,
|
||||
mem_replace::MEM_REPLACE_WITH_DEFAULT,
|
||||
|
|
|
@ -266,7 +266,6 @@ mod map_unit_fn;
|
|||
mod match_on_vec_items;
|
||||
mod match_result_ok;
|
||||
mod matches;
|
||||
mod mem_discriminant;
|
||||
mod mem_forget;
|
||||
mod mem_replace;
|
||||
mod methods;
|
||||
|
@ -600,7 +599,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||
let doc_valid_idents = conf.doc_valid_idents.iter().cloned().collect::<FxHashSet<_>>();
|
||||
store.register_late_pass(move || Box::new(doc::DocMarkdown::new(doc_valid_idents.clone())));
|
||||
store.register_late_pass(|| Box::new(neg_multiply::NegMultiply));
|
||||
store.register_late_pass(|| Box::new(mem_discriminant::MemDiscriminant));
|
||||
store.register_late_pass(|| Box::new(mem_forget::MemForget));
|
||||
store.register_late_pass(|| Box::new(arithmetic::Arithmetic::default()));
|
||||
store.register_late_pass(|| Box::new(assign_ops::AssignOps));
|
||||
|
@ -850,6 +848,7 @@ pub fn register_renamed(ls: &mut rustc_lint::LintStore) {
|
|||
ls.register_renamed("clippy::panic_params", "non_fmt_panics");
|
||||
ls.register_renamed("clippy::unknown_clippy_lints", "unknown_lints");
|
||||
ls.register_renamed("clippy::invalid_atomic_ordering", "invalid_atomic_ordering");
|
||||
ls.register_renamed("clippy::mem_discriminant_non_enum", "enum_intrinsics_non_enums");
|
||||
}
|
||||
|
||||
// only exists to let the dogfood integration test works.
|
||||
|
|
|
@ -1,82 +0,0 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_then;
|
||||
use clippy_utils::source::snippet;
|
||||
use clippy_utils::ty::walk_ptrs_ty_depth;
|
||||
use clippy_utils::{match_def_path, paths};
|
||||
use if_chain::if_chain;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{BorrowKind, Expr, ExprKind};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// ### What it does
|
||||
/// Checks for calls of `mem::discriminant()` on a non-enum type.
|
||||
///
|
||||
/// ### Why is this bad?
|
||||
/// The value of `mem::discriminant()` on non-enum types
|
||||
/// is unspecified.
|
||||
///
|
||||
/// ### Example
|
||||
/// ```rust
|
||||
/// use std::mem;
|
||||
///
|
||||
/// mem::discriminant(&"hello");
|
||||
/// mem::discriminant(&&Some(2));
|
||||
/// ```
|
||||
pub MEM_DISCRIMINANT_NON_ENUM,
|
||||
correctness,
|
||||
"calling `mem::descriminant` on non-enum type"
|
||||
}
|
||||
|
||||
declare_lint_pass!(MemDiscriminant => [MEM_DISCRIMINANT_NON_ENUM]);
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for MemDiscriminant {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
if_chain! {
|
||||
if let ExprKind::Call(func, func_args) = expr.kind;
|
||||
// is `mem::discriminant`
|
||||
if let ExprKind::Path(ref func_qpath) = func.kind;
|
||||
if let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id();
|
||||
if match_def_path(cx, def_id, &paths::MEM_DISCRIMINANT);
|
||||
// type is non-enum
|
||||
let ty_param = cx.typeck_results().node_substs(func.hir_id).type_at(0);
|
||||
if !ty_param.is_enum();
|
||||
|
||||
then {
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
MEM_DISCRIMINANT_NON_ENUM,
|
||||
expr.span,
|
||||
&format!("calling `mem::discriminant` on non-enum type `{}`", ty_param),
|
||||
|diag| {
|
||||
// if this is a reference to an enum, suggest dereferencing
|
||||
let (base_ty, ptr_depth) = walk_ptrs_ty_depth(ty_param);
|
||||
if ptr_depth >= 1 && base_ty.is_enum() {
|
||||
let param = &func_args[0];
|
||||
|
||||
// cancel out '&'s first
|
||||
let mut derefs_needed = ptr_depth;
|
||||
let mut cur_expr = param;
|
||||
while derefs_needed > 0 {
|
||||
if let ExprKind::AddrOf(BorrowKind::Ref, _, inner_expr) = cur_expr.kind {
|
||||
derefs_needed -= 1;
|
||||
cur_expr = inner_expr;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let derefs = "*".repeat(derefs_needed);
|
||||
diag.span_suggestion(
|
||||
param.span,
|
||||
"try dereferencing",
|
||||
format!("{}{}", derefs, snippet(cx, cur_expr.span, "<param>")),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
// run-rustfix
|
||||
|
||||
#![deny(clippy::mem_discriminant_non_enum)]
|
||||
|
||||
use std::mem;
|
||||
|
||||
enum Foo {
|
||||
One(usize),
|
||||
Two(u8),
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// bad
|
||||
mem::discriminant(&Some(2));
|
||||
mem::discriminant(&None::<u8>);
|
||||
mem::discriminant(&Foo::One(5));
|
||||
mem::discriminant(&Foo::Two(5));
|
||||
|
||||
let ro = &Some(3);
|
||||
let rro = &ro;
|
||||
mem::discriminant(ro);
|
||||
mem::discriminant(*rro);
|
||||
mem::discriminant(*rro);
|
||||
|
||||
macro_rules! mem_discriminant_but_in_a_macro {
|
||||
($param:expr) => {
|
||||
mem::discriminant($param)
|
||||
};
|
||||
}
|
||||
|
||||
mem_discriminant_but_in_a_macro!(*rro);
|
||||
|
||||
let rrrrro = &&&rro;
|
||||
mem::discriminant(****rrrrro);
|
||||
mem::discriminant(****rrrrro);
|
||||
|
||||
// ok
|
||||
mem::discriminant(&Some(2));
|
||||
mem::discriminant(&None::<u8>);
|
||||
mem::discriminant(&Foo::One(5));
|
||||
mem::discriminant(&Foo::Two(5));
|
||||
mem::discriminant(ro);
|
||||
mem::discriminant(*rro);
|
||||
mem::discriminant(****rrrrro);
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
// run-rustfix
|
||||
|
||||
#![deny(clippy::mem_discriminant_non_enum)]
|
||||
|
||||
use std::mem;
|
||||
|
||||
enum Foo {
|
||||
One(usize),
|
||||
Two(u8),
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// bad
|
||||
mem::discriminant(&&Some(2));
|
||||
mem::discriminant(&&None::<u8>);
|
||||
mem::discriminant(&&Foo::One(5));
|
||||
mem::discriminant(&&Foo::Two(5));
|
||||
|
||||
let ro = &Some(3);
|
||||
let rro = &ro;
|
||||
mem::discriminant(&ro);
|
||||
mem::discriminant(rro);
|
||||
mem::discriminant(&rro);
|
||||
|
||||
macro_rules! mem_discriminant_but_in_a_macro {
|
||||
($param:expr) => {
|
||||
mem::discriminant($param)
|
||||
};
|
||||
}
|
||||
|
||||
mem_discriminant_but_in_a_macro!(&rro);
|
||||
|
||||
let rrrrro = &&&rro;
|
||||
mem::discriminant(&rrrrro);
|
||||
mem::discriminant(*rrrrro);
|
||||
|
||||
// ok
|
||||
mem::discriminant(&Some(2));
|
||||
mem::discriminant(&None::<u8>);
|
||||
mem::discriminant(&Foo::One(5));
|
||||
mem::discriminant(&Foo::Two(5));
|
||||
mem::discriminant(ro);
|
||||
mem::discriminant(*rro);
|
||||
mem::discriminant(****rrrrro);
|
||||
}
|
|
@ -1,94 +0,0 @@
|
|||
error: calling `mem::discriminant` on non-enum type `&std::option::Option<i32>`
|
||||
--> $DIR/mem_discriminant.rs:14:5
|
||||
|
|
||||
LL | mem::discriminant(&&Some(2));
|
||||
| ^^^^^^^^^^^^^^^^^^---------^
|
||||
| |
|
||||
| help: try dereferencing: `&Some(2)`
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/mem_discriminant.rs:3:9
|
||||
|
|
||||
LL | #![deny(clippy::mem_discriminant_non_enum)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: calling `mem::discriminant` on non-enum type `&std::option::Option<u8>`
|
||||
--> $DIR/mem_discriminant.rs:15:5
|
||||
|
|
||||
LL | mem::discriminant(&&None::<u8>);
|
||||
| ^^^^^^^^^^^^^^^^^^------------^
|
||||
| |
|
||||
| help: try dereferencing: `&None::<u8>`
|
||||
|
||||
error: calling `mem::discriminant` on non-enum type `&Foo`
|
||||
--> $DIR/mem_discriminant.rs:16:5
|
||||
|
|
||||
LL | mem::discriminant(&&Foo::One(5));
|
||||
| ^^^^^^^^^^^^^^^^^^-------------^
|
||||
| |
|
||||
| help: try dereferencing: `&Foo::One(5)`
|
||||
|
||||
error: calling `mem::discriminant` on non-enum type `&Foo`
|
||||
--> $DIR/mem_discriminant.rs:17:5
|
||||
|
|
||||
LL | mem::discriminant(&&Foo::Two(5));
|
||||
| ^^^^^^^^^^^^^^^^^^-------------^
|
||||
| |
|
||||
| help: try dereferencing: `&Foo::Two(5)`
|
||||
|
||||
error: calling `mem::discriminant` on non-enum type `&std::option::Option<i32>`
|
||||
--> $DIR/mem_discriminant.rs:21:5
|
||||
|
|
||||
LL | mem::discriminant(&ro);
|
||||
| ^^^^^^^^^^^^^^^^^^---^
|
||||
| |
|
||||
| help: try dereferencing: `ro`
|
||||
|
||||
error: calling `mem::discriminant` on non-enum type `&std::option::Option<i32>`
|
||||
--> $DIR/mem_discriminant.rs:22:5
|
||||
|
|
||||
LL | mem::discriminant(rro);
|
||||
| ^^^^^^^^^^^^^^^^^^---^
|
||||
| |
|
||||
| help: try dereferencing: `*rro`
|
||||
|
||||
error: calling `mem::discriminant` on non-enum type `&&std::option::Option<i32>`
|
||||
--> $DIR/mem_discriminant.rs:23:5
|
||||
|
|
||||
LL | mem::discriminant(&rro);
|
||||
| ^^^^^^^^^^^^^^^^^^----^
|
||||
| |
|
||||
| help: try dereferencing: `*rro`
|
||||
|
||||
error: calling `mem::discriminant` on non-enum type `&&std::option::Option<i32>`
|
||||
--> $DIR/mem_discriminant.rs:27:13
|
||||
|
|
||||
LL | mem::discriminant($param)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
...
|
||||
LL | mem_discriminant_but_in_a_macro!(&rro);
|
||||
| ---------------------------------------
|
||||
| | |
|
||||
| | help: try dereferencing: `*rro`
|
||||
| in this macro invocation
|
||||
|
|
||||
= note: this error originates in the macro `mem_discriminant_but_in_a_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error: calling `mem::discriminant` on non-enum type `&&&&&std::option::Option<i32>`
|
||||
--> $DIR/mem_discriminant.rs:34:5
|
||||
|
|
||||
LL | mem::discriminant(&rrrrro);
|
||||
| ^^^^^^^^^^^^^^^^^^-------^
|
||||
| |
|
||||
| help: try dereferencing: `****rrrrro`
|
||||
|
||||
error: calling `mem::discriminant` on non-enum type `&&&std::option::Option<i32>`
|
||||
--> $DIR/mem_discriminant.rs:35:5
|
||||
|
|
||||
LL | mem::discriminant(*rrrrro);
|
||||
| ^^^^^^^^^^^^^^^^^^-------^
|
||||
| |
|
||||
| help: try dereferencing: `****rrrrro`
|
||||
|
||||
error: aborting due to 10 previous errors
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
#![deny(clippy::mem_discriminant_non_enum)]
|
||||
|
||||
use std::mem;
|
||||
|
||||
enum Foo {
|
||||
One(usize),
|
||||
Two(u8),
|
||||
}
|
||||
|
||||
struct A(Foo);
|
||||
|
||||
fn main() {
|
||||
// bad
|
||||
mem::discriminant(&"hello");
|
||||
mem::discriminant(&A(Foo::One(0)));
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
error: calling `mem::discriminant` on non-enum type `&str`
|
||||
--> $DIR/mem_discriminant_unfixable.rs:14:5
|
||||
|
|
||||
LL | mem::discriminant(&"hello");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/mem_discriminant_unfixable.rs:1:9
|
||||
|
|
||||
LL | #![deny(clippy::mem_discriminant_non_enum)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: calling `mem::discriminant` on non-enum type `A`
|
||||
--> $DIR/mem_discriminant_unfixable.rs:15:5
|
||||
|
|
||||
LL | mem::discriminant(&A(Foo::One(0)));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
|
@ -8,6 +8,7 @@
|
|||
#![allow(clippy::redundant_static_lifetimes)]
|
||||
// warn for the old lint name here, to test if the renaming worked
|
||||
#![warn(clippy::cognitive_complexity)]
|
||||
#![warn(enum_intrinsics_non_enums)]
|
||||
|
||||
#[warn(clippy::module_name_repetitions)]
|
||||
fn main() {}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#![allow(clippy::redundant_static_lifetimes)]
|
||||
// warn for the old lint name here, to test if the renaming worked
|
||||
#![warn(clippy::cyclomatic_complexity)]
|
||||
#![warn(clippy::mem_discriminant_non_enum)]
|
||||
|
||||
#[warn(clippy::stutter)]
|
||||
fn main() {}
|
||||
|
|
|
@ -6,23 +6,29 @@ LL | #![warn(clippy::cyclomatic_complexity)]
|
|||
|
|
||||
= note: `-D renamed-and-removed-lints` implied by `-D warnings`
|
||||
|
||||
error: lint `clippy::mem_discriminant_non_enum` has been renamed to `enum_intrinsics_non_enums`
|
||||
--> $DIR/rename.rs:11:9
|
||||
|
|
||||
LL | #![warn(clippy::mem_discriminant_non_enum)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `enum_intrinsics_non_enums`
|
||||
|
||||
error: lint `clippy::stutter` has been renamed to `clippy::module_name_repetitions`
|
||||
--> $DIR/rename.rs:12:8
|
||||
--> $DIR/rename.rs:13:8
|
||||
|
|
||||
LL | #[warn(clippy::stutter)]
|
||||
| ^^^^^^^^^^^^^^^ help: use the new name: `clippy::module_name_repetitions`
|
||||
|
||||
error: lint `clippy::new_without_default_derive` has been renamed to `clippy::new_without_default`
|
||||
--> $DIR/rename.rs:15:8
|
||||
--> $DIR/rename.rs:16:8
|
||||
|
|
||||
LL | #[warn(clippy::new_without_default_derive)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::new_without_default`
|
||||
|
||||
error: lint `clippy::const_static_lifetime` has been renamed to `clippy::redundant_static_lifetimes`
|
||||
--> $DIR/rename.rs:18:8
|
||||
--> $DIR/rename.rs:19:8
|
||||
|
|
||||
LL | #[warn(clippy::const_static_lifetime)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::redundant_static_lifetimes`
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
error: aborting due to 5 previous errors
|
||||
|
||||
|
|
Loading…
Reference in a new issue