2022-06-30 03:50:09 -05:00
|
|
|
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_hir_and_then};
|
2023-03-24 08:04:35 -05:00
|
|
|
use clippy_utils::source::{snippet, snippet_opt, snippet_with_context};
|
2018-11-27 14:14:15 -06:00
|
|
|
use if_chain::if_chain;
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-09 01:13:22 -06:00
|
|
|
use rustc_hir::intravisit::FnKind;
|
2020-02-21 02:39:38 -06:00
|
|
|
use rustc_hir::{
|
2023-01-22 12:00:33 -06:00
|
|
|
self as hir, def, BinOpKind, BindingAnnotation, Body, ByRef, Expr, ExprKind, FnDecl, Mutability, PatKind, Stmt,
|
|
|
|
StmtKind, TyKind,
|
2020-02-21 02:39:38 -06:00
|
|
|
};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-11-05 07:29:48 -06:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
2022-12-17 07:12:54 -06:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2023-01-22 12:00:33 -06:00
|
|
|
use rustc_span::def_id::LocalDefId;
|
2020-04-26 17:12:51 -05:00
|
|
|
use rustc_span::hygiene::DesugaringKind;
|
2020-01-04 04:00:00 -06:00
|
|
|
use rustc_span::source_map::{ExpnKind, Span};
|
2015-05-06 03:01:49 -05:00
|
|
|
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::sugg::Sugg;
|
2022-12-17 07:12:54 -06:00
|
|
|
use clippy_utils::{
|
2023-05-20 08:39:26 -05:00
|
|
|
get_parent_expr, in_constant, is_integer_literal, is_lint_allowed, is_no_std_crate, iter_input_pats,
|
|
|
|
last_path_segment, SpanlessEq,
|
2022-12-17 07:12:54 -06:00
|
|
|
};
|
2019-01-30 19:15:29 -06:00
|
|
|
|
2023-05-20 08:39:26 -05:00
|
|
|
use crate::ref_patterns::REF_PATTERNS;
|
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for function arguments and let bindings denoted as
|
2019-03-05 10:50:33 -06:00
|
|
|
/// `ref`.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// The `ref` declaration makes the function take an owned
|
2019-03-05 10:50:33 -06:00
|
|
|
/// value, but turns the argument into a reference (which means that the value
|
|
|
|
/// is destroyed when exiting the function). This adds not much value: either
|
|
|
|
/// take a reference type, or take an owned value and create references in the
|
|
|
|
/// body.
|
|
|
|
///
|
|
|
|
/// For let bindings, `let x = &foo;` is preferred over `let ref x = foo`. The
|
|
|
|
/// type of `x` is more obvious with the former.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Known problems
|
|
|
|
/// If the argument is dereferenced within the function,
|
2019-03-05 10:50:33 -06:00
|
|
|
/// removing the `ref` will lead to errors. This can be fixed by removing the
|
2019-01-30 19:15:29 -06:00
|
|
|
/// dereferences, e.g., changing `*x` to `x` within the function.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2022-06-16 10:39:06 -05:00
|
|
|
/// ```rust
|
|
|
|
/// fn foo(ref _x: u8) {}
|
|
|
|
/// ```
|
2020-06-09 09:36:01 -05:00
|
|
|
///
|
2022-06-16 10:39:06 -05:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// fn foo(_x: &u8) {}
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-08-06 03:18:36 -05:00
|
|
|
pub TOPLEVEL_REF_ARG,
|
2018-03-28 08:24:26 -05:00
|
|
|
style,
|
2016-08-06 03:18:36 -05:00
|
|
|
"an entire binding declared as `ref`, in a function argument or a `let` statement"
|
2016-02-05 17:13:29 -06:00
|
|
|
}
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for the use of bindings with a single leading
|
2019-03-05 10:50:33 -06:00
|
|
|
/// underscore.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// A single leading underscore is usually used to indicate
|
2019-03-05 10:50:33 -06:00
|
|
|
/// that a binding will not be used. Using such a binding breaks this
|
|
|
|
/// expectation.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Known problems
|
|
|
|
/// The lint does not work properly with desugaring and
|
2019-03-05 10:50:33 -06:00
|
|
|
/// macro, it has been allowed in the mean time.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
|
|
|
/// let _x = 0;
|
|
|
|
/// let y = _x + 1; // Here we are using `_x`, even though it has a leading
|
|
|
|
/// // underscore. We should rename `_x` to `x`
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-08-24 14:47:46 -05:00
|
|
|
pub USED_UNDERSCORE_BINDING,
|
2018-03-28 08:24:26 -05:00
|
|
|
pedantic,
|
2016-08-24 14:47:46 -05:00
|
|
|
"using a binding which is prefixed with an underscore"
|
|
|
|
}
|
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for the use of short circuit boolean conditions as
|
2019-03-05 10:50:33 -06:00
|
|
|
/// a
|
|
|
|
/// statement.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Using a short circuit boolean condition as a statement
|
2019-03-05 10:50:33 -06:00
|
|
|
/// may hide the fact that the second part is executed or not depending on the
|
|
|
|
/// outcome of the first part.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-08-02 01:13:54 -05:00
|
|
|
/// ```rust,ignore
|
2019-03-05 10:50:33 -06:00
|
|
|
/// f() && g(); // We should write `if f() { g(); }`.
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-12-29 17:00:55 -06:00
|
|
|
pub SHORT_CIRCUIT_STATEMENT,
|
2018-03-28 08:24:26 -05:00
|
|
|
complexity,
|
2016-12-29 17:00:55 -06:00
|
|
|
"using a short circuit boolean condition as a statement"
|
|
|
|
}
|
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Catch casts from `0` to some pointer type
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// This generally means `null` and is better expressed as
|
2019-03-05 10:50:33 -06:00
|
|
|
/// {`std`, `core`}`::ptr::`{`null`, `null_mut`}.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-09 01:51:23 -06:00
|
|
|
/// ```rust
|
|
|
|
/// let a = 0 as *const u32;
|
2022-06-16 10:39:06 -05:00
|
|
|
/// ```
|
2020-06-09 09:36:01 -05:00
|
|
|
///
|
2022-06-16 10:39:06 -05:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
2020-06-09 09:36:01 -05:00
|
|
|
/// let a = std::ptr::null::<u32>();
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2017-03-07 05:58:07 -06:00
|
|
|
pub ZERO_PTR,
|
2018-03-28 08:24:26 -05:00
|
|
|
style,
|
2020-01-06 00:30:43 -06:00
|
|
|
"using `0 as *{const, mut} T`"
|
2017-03-07 05:58:07 -06:00
|
|
|
}
|
|
|
|
|
2022-12-17 07:12:54 -06:00
|
|
|
pub struct LintPass {
|
|
|
|
std_or_core: &'static str,
|
|
|
|
}
|
|
|
|
impl Default for LintPass {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self { std_or_core: "std" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl_lint_pass!(LintPass => [
|
2019-04-08 15:43:55 -05:00
|
|
|
TOPLEVEL_REF_ARG,
|
|
|
|
USED_UNDERSCORE_BINDING,
|
|
|
|
SHORT_CIRCUIT_STATEMENT,
|
|
|
|
ZERO_PTR,
|
|
|
|
]);
|
2014-12-24 17:15:22 -06:00
|
|
|
|
2022-12-17 07:12:54 -06:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for LintPass {
|
|
|
|
fn check_crate(&mut self, cx: &LateContext<'_>) {
|
|
|
|
if is_no_std_crate(cx) {
|
|
|
|
self.std_or_core = "core";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 05:14:54 -06:00
|
|
|
fn check_fn(
|
|
|
|
&mut self,
|
2020-06-25 15:41:36 -05:00
|
|
|
cx: &LateContext<'tcx>,
|
2016-12-21 05:14:54 -06:00
|
|
|
k: FnKind<'tcx>,
|
2019-12-29 22:02:10 -06:00
|
|
|
decl: &'tcx FnDecl<'_>,
|
2019-12-22 08:42:41 -06:00
|
|
|
body: &'tcx Body<'_>,
|
2020-11-05 07:29:48 -06:00
|
|
|
span: Span,
|
2023-01-22 12:00:33 -06:00
|
|
|
_: LocalDefId,
|
2016-12-21 05:14:54 -06:00
|
|
|
) {
|
2020-11-27 02:24:42 -06:00
|
|
|
if let FnKind::Closure = k {
|
2015-08-16 06:54:03 -05:00
|
|
|
// Does not apply to closures
|
2016-01-03 22:26:12 -06:00
|
|
|
return;
|
2015-08-16 06:54:03 -05:00
|
|
|
}
|
2020-11-05 07:29:48 -06:00
|
|
|
if in_external_macro(cx.tcx.sess, span) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-04 15:46:41 -06:00
|
|
|
for arg in iter_input_pats(decl, body) {
|
2023-05-20 08:39:26 -05:00
|
|
|
// Do not emit if clippy::ref_patterns is not allowed to avoid having two lints for the same issue.
|
|
|
|
if !is_lint_allowed(cx, REF_PATTERNS, arg.pat.hir_id) {
|
|
|
|
return;
|
|
|
|
}
|
2022-08-30 17:36:53 -05:00
|
|
|
if let PatKind::Binding(BindingAnnotation(ByRef::Yes, _), ..) = arg.pat.kind {
|
2020-06-09 09:36:01 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
TOPLEVEL_REF_ARG,
|
|
|
|
arg.pat.span,
|
|
|
|
"`ref` directly on a function argument is ignored. \
|
2021-03-12 08:30:50 -06:00
|
|
|
Consider using a reference type instead",
|
2020-06-09 09:36:01 -05:00
|
|
|
);
|
2014-12-24 17:15:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-24 14:47:46 -05:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
|
2017-10-23 14:18:02 -05:00
|
|
|
if_chain! {
|
2020-11-05 07:29:48 -06:00
|
|
|
if !in_external_macro(cx.tcx.sess, stmt.span);
|
2021-04-08 10:50:13 -05:00
|
|
|
if let StmtKind::Local(local) = stmt.kind;
|
2022-08-30 17:36:53 -05:00
|
|
|
if let PatKind::Binding(BindingAnnotation(ByRef::Yes, mutabl), .., name, None) = local.pat.kind;
|
2021-04-08 10:50:13 -05:00
|
|
|
if let Some(init) = local.init;
|
2023-05-20 08:39:26 -05:00
|
|
|
// Do not emit if clippy::ref_patterns is not allowed to avoid having two lints for the same issue.
|
|
|
|
if is_lint_allowed(cx, REF_PATTERNS, local.pat.hir_id);
|
2017-10-23 14:18:02 -05:00
|
|
|
then {
|
2023-03-24 08:04:35 -05:00
|
|
|
let ctxt = local.span.ctxt();
|
|
|
|
let mut app = Applicability::MachineApplicable;
|
|
|
|
let sugg_init = Sugg::hir_with_context(cx, init, ctxt, "..", &mut app);
|
2022-08-30 17:36:53 -05:00
|
|
|
let (mutopt, initref) = if mutabl == Mutability::Mut {
|
2021-04-08 10:50:13 -05:00
|
|
|
("mut ", sugg_init.mut_addr())
|
|
|
|
} else {
|
|
|
|
("", sugg_init.addr())
|
|
|
|
};
|
|
|
|
let tyopt = if let Some(ty) = local.ty {
|
2023-03-24 08:04:35 -05:00
|
|
|
let ty_snip = snippet_with_context(cx, ty.span, ctxt, "_", &mut app).0;
|
|
|
|
format!(": &{mutopt}{ty_snip}")
|
2021-04-08 10:50:13 -05:00
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
|
|
|
span_lint_hir_and_then(
|
|
|
|
cx,
|
|
|
|
TOPLEVEL_REF_ARG,
|
|
|
|
init.hir_id,
|
|
|
|
local.pat.span,
|
|
|
|
"`ref` on an entire `let` pattern is discouraged, take a reference with `&` instead",
|
|
|
|
|diag| {
|
|
|
|
diag.span_suggestion(
|
|
|
|
stmt.span,
|
|
|
|
"try",
|
|
|
|
format!(
|
|
|
|
"let {name}{tyopt} = {initref};",
|
|
|
|
name=snippet(cx, name.span, ".."),
|
|
|
|
),
|
2023-03-24 08:04:35 -05:00
|
|
|
app,
|
2021-04-08 10:50:13 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2017-10-23 14:18:02 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if_chain! {
|
2021-04-08 10:50:13 -05:00
|
|
|
if let StmtKind::Semi(expr) = stmt.kind;
|
|
|
|
if let ExprKind::Binary(ref binop, a, b) = expr.kind;
|
2018-07-12 02:50:09 -05:00
|
|
|
if binop.node == BinOpKind::And || binop.node == BinOpKind::Or;
|
2017-10-23 14:18:02 -05:00
|
|
|
if let Some(sugg) = Sugg::hir_opt(cx, a);
|
|
|
|
then {
|
2021-06-03 01:41:37 -05:00
|
|
|
span_lint_hir_and_then(
|
|
|
|
cx,
|
2017-10-23 14:18:02 -05:00
|
|
|
SHORT_CIRCUIT_STATEMENT,
|
2021-06-03 01:41:37 -05:00
|
|
|
expr.hir_id,
|
2019-09-25 20:46:51 -05:00
|
|
|
stmt.span,
|
2017-10-23 14:18:02 -05:00
|
|
|
"boolean short circuit operator in statement may be clearer using an explicit test",
|
2020-04-17 01:08:00 -05:00
|
|
|
|diag| {
|
2018-07-12 02:50:09 -05:00
|
|
|
let sugg = if binop.node == BinOpKind::Or { !sugg } else { sugg };
|
2020-04-17 01:08:00 -05:00
|
|
|
diag.span_suggestion(
|
2019-09-25 20:46:51 -05:00
|
|
|
stmt.span,
|
2018-09-18 10:07:54 -05:00
|
|
|
"replace it with",
|
|
|
|
format!(
|
2022-10-06 02:44:38 -05:00
|
|
|
"if {sugg} {{ {}; }}",
|
2018-09-18 10:07:54 -05:00
|
|
|
&snippet(cx, b.span, ".."),
|
|
|
|
),
|
2018-09-18 12:01:17 -05:00
|
|
|
Applicability::MachineApplicable, // snippet
|
2018-09-18 10:07:54 -05:00
|
|
|
);
|
2017-10-23 14:18:02 -05:00
|
|
|
});
|
2017-08-01 02:11:05 -05:00
|
|
|
}
|
2017-10-23 14:18:02 -05:00
|
|
|
};
|
2015-09-22 02:08:42 -05:00
|
|
|
}
|
2015-05-04 07:11:15 -05:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2022-06-30 03:50:09 -05:00
|
|
|
if let ExprKind::Cast(e, ty) = expr.kind {
|
2022-12-17 07:12:54 -06:00
|
|
|
self.check_cast(cx, expr.span, e, ty);
|
2022-06-30 03:50:09 -05:00
|
|
|
return;
|
2016-08-24 14:47:46 -05:00
|
|
|
}
|
2020-04-26 17:12:51 -05:00
|
|
|
if in_attributes_expansion(expr) || expr.span.is_desugaring(DesugaringKind::Await) {
|
|
|
|
// Don't lint things expanded by #[derive(...)], etc or `await` desugaring
|
2016-08-24 14:47:46 -05:00
|
|
|
return;
|
|
|
|
}
|
2021-12-14 15:32:21 -06:00
|
|
|
let sym;
|
2019-09-27 10:16:06 -05:00
|
|
|
let binding = match expr.kind {
|
2020-08-28 09:10:16 -05:00
|
|
|
ExprKind::Path(ref qpath) if !matches!(qpath, hir::QPath::LangItem(..)) => {
|
2018-06-28 08:46:58 -05:00
|
|
|
let binding = last_path_segment(qpath).ident.as_str();
|
2016-12-02 10:38:31 -06:00
|
|
|
if binding.starts_with('_') &&
|
|
|
|
!binding.starts_with("__") &&
|
2017-03-09 03:58:31 -06:00
|
|
|
binding != "_result" && // FIXME: #944
|
2016-12-02 10:38:31 -06:00
|
|
|
is_used(cx, expr) &&
|
|
|
|
// don't lint if the declaration is in a macro
|
2020-06-25 21:55:23 -05:00
|
|
|
non_macro_local(cx, cx.qpath_res(qpath, expr.hir_id))
|
2017-08-09 02:30:56 -05:00
|
|
|
{
|
2016-12-02 10:38:31 -06:00
|
|
|
Some(binding)
|
2016-08-24 14:47:46 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:30:57 -05:00
|
|
|
ExprKind::Field(_, ident) => {
|
2021-12-14 15:32:21 -06:00
|
|
|
sym = ident.name;
|
|
|
|
let name = sym.as_str();
|
2016-08-24 14:47:46 -05:00
|
|
|
if name.starts_with('_') && !name.starts_with("__") {
|
|
|
|
Some(name)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-08-24 14:47:46 -05:00
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
if let Some(binding) = binding {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
USED_UNDERSCORE_BINDING,
|
|
|
|
expr.span,
|
|
|
|
&format!(
|
2022-10-06 02:44:38 -05:00
|
|
|
"used binding `{binding}` which is prefixed with an underscore. A leading \
|
|
|
|
underscore signals that a binding will not be used"
|
2017-08-09 02:30:56 -05:00
|
|
|
),
|
|
|
|
);
|
2016-08-24 14:47:46 -05:00
|
|
|
}
|
|
|
|
}
|
2015-05-06 03:01:49 -05:00
|
|
|
}
|
|
|
|
|
2017-08-09 02:30:56 -05:00
|
|
|
/// Heuristic to see if an expression is used. Should be compatible with
|
|
|
|
/// `unused_variables`'s idea
|
2015-12-21 03:03:12 -06:00
|
|
|
/// of what it means for an expression to be "used".
|
2020-06-25 15:41:36 -05:00
|
|
|
fn is_used(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
2020-07-14 07:59:59 -05:00
|
|
|
get_parent_expr(cx, expr).map_or(true, |parent| match parent.kind {
|
2021-04-08 10:50:13 -05:00
|
|
|
ExprKind::Assign(_, rhs, _) | ExprKind::AssignOp(_, _, rhs) => SpanlessEq::new(cx).eq_expr(rhs, expr),
|
2020-07-14 07:59:59 -05:00
|
|
|
_ => is_used(cx, parent),
|
|
|
|
})
|
2015-12-18 18:04:33 -06:00
|
|
|
}
|
2015-12-21 03:03:12 -06:00
|
|
|
|
2019-01-30 19:15:29 -06:00
|
|
|
/// Tests whether an expression is in a macro expansion (e.g., something
|
2019-03-10 12:19:47 -05:00
|
|
|
/// generated by `#[derive(...)]` or the like).
|
2019-12-27 01:12:26 -06:00
|
|
|
fn in_attributes_expansion(expr: &Expr<'_>) -> bool {
|
2019-12-30 18:17:56 -06:00
|
|
|
use rustc_span::hygiene::MacroKind;
|
2019-08-16 11:29:30 -05:00
|
|
|
if expr.span.from_expansion() {
|
|
|
|
let data = expr.span.ctxt().outer_expn_data();
|
2022-07-18 02:39:37 -05:00
|
|
|
matches!(data.kind, ExpnKind::Macro(MacroKind::Attr | MacroKind::Derive, _))
|
2019-08-16 11:29:30 -05:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2015-12-21 03:03:12 -06:00
|
|
|
}
|
2016-06-15 09:27:56 -05:00
|
|
|
|
2019-05-03 19:03:12 -05:00
|
|
|
/// Tests whether `res` is a variable defined outside a macro.
|
2020-06-25 15:41:36 -05:00
|
|
|
fn non_macro_local(cx: &LateContext<'_>, res: def::Res) -> bool {
|
2019-06-02 11:30:40 -05:00
|
|
|
if let def::Res::Local(id) = res {
|
2019-08-19 11:30:32 -05:00
|
|
|
!cx.tcx.hir().span(id).from_expansion()
|
2019-06-02 11:30:40 -05:00
|
|
|
} else {
|
|
|
|
false
|
2016-06-15 09:27:56 -05:00
|
|
|
}
|
|
|
|
}
|
2017-03-07 05:58:07 -06:00
|
|
|
|
2022-12-17 07:12:54 -06:00
|
|
|
impl LintPass {
|
|
|
|
fn check_cast(&self, cx: &LateContext<'_>, span: Span, e: &Expr<'_>, ty: &hir::Ty<'_>) {
|
|
|
|
if_chain! {
|
|
|
|
if let TyKind::Ptr(ref mut_ty) = ty.kind;
|
|
|
|
if is_integer_literal(e, 0);
|
|
|
|
if !in_constant(cx, e.hir_id);
|
|
|
|
then {
|
|
|
|
let (msg, sugg_fn) = match mut_ty.mutbl {
|
|
|
|
Mutability::Mut => ("`0 as *mut _` detected", "ptr::null_mut"),
|
|
|
|
Mutability::Not => ("`0 as *const _` detected", "ptr::null"),
|
|
|
|
};
|
2019-10-02 10:38:00 -05:00
|
|
|
|
2022-12-17 07:12:54 -06:00
|
|
|
let (sugg, appl) = if let TyKind::Infer = mut_ty.ty.kind {
|
|
|
|
(format!("{}::{sugg_fn}()", self.std_or_core), Applicability::MachineApplicable)
|
|
|
|
} else if let Some(mut_ty_snip) = snippet_opt(cx, mut_ty.ty.span) {
|
|
|
|
(format!("{}::{sugg_fn}::<{mut_ty_snip}>()", self.std_or_core), Applicability::MachineApplicable)
|
|
|
|
} else {
|
|
|
|
// `MaybeIncorrect` as type inference may not work with the suggested code
|
|
|
|
(format!("{}::{sugg_fn}()", self.std_or_core), Applicability::MaybeIncorrect)
|
|
|
|
};
|
|
|
|
span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", sugg, appl);
|
|
|
|
}
|
2017-10-23 14:18:02 -05:00
|
|
|
}
|
|
|
|
}
|
2017-03-07 05:58:07 -06:00
|
|
|
}
|