2021-03-16 00:55:45 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2021-03-14 23:17:44 +00:00
|
|
|
use clippy_utils::source::snippet;
|
2021-03-13 23:01:03 +00:00
|
|
|
use clippy_utils::ty::walk_ptrs_ty_depth;
|
2021-03-16 16:06:34 +00:00
|
|
|
use clippy_utils::{match_def_path, paths};
|
2018-10-21 03:46:13 +00:00
|
|
|
use if_chain::if_chain;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir::{BorrowKind, Expr, ExprKind};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2018-10-21 03:46:13 +00:00
|
|
|
use std::iter;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **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.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2019-03-05 22:23:50 +00:00
|
|
|
/// use std::mem;
|
|
|
|
///
|
2019-03-05 16:50:33 +00:00
|
|
|
/// mem::discriminant(&"hello");
|
|
|
|
/// mem::discriminant(&&Some(2));
|
|
|
|
/// ```
|
2018-10-21 03:46:13 +00:00
|
|
|
pub MEM_DISCRIMINANT_NON_ENUM,
|
|
|
|
correctness,
|
2020-01-06 06:30:43 +00:00
|
|
|
"calling `mem::descriminant` on non-enum type"
|
2018-10-21 03:46:13 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(MemDiscriminant => [MEM_DISCRIMINANT_NON_ENUM]);
|
2018-10-21 03:46:13 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for MemDiscriminant {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2018-10-21 03:46:13 +00:00
|
|
|
if_chain! {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Call(ref func, ref func_args) = expr.kind;
|
2018-10-21 03:46:13 +00:00
|
|
|
// is `mem::discriminant`
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Path(ref func_qpath) = func.kind;
|
2020-06-26 02:55:23 +00:00
|
|
|
if let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id();
|
2019-05-17 21:53:54 +00:00
|
|
|
if match_def_path(cx, def_id, &paths::MEM_DISCRIMINANT);
|
2018-10-21 03:46:13 +00:00
|
|
|
// type is non-enum
|
2020-07-17 08:47:04 +00:00
|
|
|
let ty_param = cx.typeck_results().node_substs(func.hir_id).type_at(0);
|
2018-10-21 03:46:13 +00:00
|
|
|
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),
|
2020-04-17 06:08:00 +00:00
|
|
|
|diag| {
|
2018-10-21 03:46:13 +00:00
|
|
|
// 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 {
|
2019-11-27 22:30:10 +00:00
|
|
|
if let ExprKind::AddrOf(BorrowKind::Ref, _, ref inner_expr) = cur_expr.kind {
|
2018-10-21 03:46:13 +00:00
|
|
|
derefs_needed -= 1;
|
|
|
|
cur_expr = inner_expr;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let derefs: String = iter::repeat('*').take(derefs_needed).collect();
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.span_suggestion(
|
2018-10-21 03:46:13 +00:00
|
|
|
param.span,
|
|
|
|
"try dereferencing",
|
|
|
|
format!("{}{}", derefs, snippet(cx, cur_expr.span, "<param>")),
|
2018-10-21 19:27:01 +00:00
|
|
|
Applicability::MachineApplicable,
|
2018-10-21 03:46:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|