use clippy_utils::ptr::get_spans;
use clippy_utils::source::snippet_opt;
use clippy_utils::ty::{is_type_diagnostic_item, match_type, walk_ptrs_hir_ty};
-use clippy_utils::{expr_path_res, is_lint_allowed, match_any_def_paths, paths};
+use clippy_utils::{expr_path_res, is_lint_allowed, match_any_diagnostic_items, paths};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{
use std::borrow::Cow;
declare_clippy_lint! {
- /// **What it does:** This lint checks for function arguments of type `&String`
+ /// ### What it does
+ /// This lint checks for function arguments of type `&String`
/// or `&Vec` unless the references are mutable. It will also suggest you
/// replace `.clone()` calls with the appropriate `.to_owned()`/`to_string()`
/// calls.
///
- /// **Why is this bad?** Requiring the argument to be of the specific size
+ /// ### Why is this bad?
+ /// Requiring the argument to be of the specific size
/// makes the function less useful for no benefit; slices in the form of `&[T]`
/// or `&str` usually suffice and can be obtained from other types, too.
///
- /// **Known problems:** The lint does not follow data. So if you have an
+ /// ### Known problems
+ /// The lint does not follow data. So if you have an
/// argument `x` and write `let y = x; y.clone()` the lint will not suggest
/// changing that `.clone()` to `.to_owned()`.
///
/// other crates referencing it, of which you may not be aware. Carefully
/// deprecate the function before applying the lint suggestions in this case.
///
- /// **Example:**
+ /// ### Example
/// ```ignore
/// // Bad
/// fn foo(&Vec<u32>) { .. }
}
declare_clippy_lint! {
- /// **What it does:** This lint checks for equality comparisons with `ptr::null`
+ /// ### What it does
+ /// This lint checks for equality comparisons with `ptr::null`
///
- /// **Why is this bad?** It's easier and more readable to use the inherent
+ /// ### Why is this bad?
+ /// It's easier and more readable to use the inherent
/// `.is_null()`
/// method instead
///
- /// **Known problems:** None.
- ///
- /// **Example:**
+ /// ### Example
/// ```ignore
/// // Bad
/// if x == ptr::null {
}
declare_clippy_lint! {
- /// **What it does:** This lint checks for functions that take immutable
+ /// ### What it does
+ /// This lint checks for functions that take immutable
/// references and return mutable ones.
///
- /// **Why is this bad?** This is trivially unsound, as one can create two
+ /// ### Why is this bad?
+ /// This is trivially unsound, as one can create two
/// mutable references from the same (immutable!) source.
/// This [error](https://github.com/rust-lang/rust/issues/39465)
/// actually lead to an interim Rust release 1.15.1.
///
- /// **Known problems:** To be on the conservative side, if there's at least one
+ /// ### Known problems
+ /// To be on the conservative side, if there's at least one
/// mutable reference with the output lifetime, this lint will not trigger.
/// In practice, this case is unlikely anyway.
///
- /// **Example:**
+ /// ### Example
/// ```ignore
/// fn foo(&Foo) -> &mut Bar { .. }
/// ```
}
declare_clippy_lint! {
- /// **What it does:** This lint checks for invalid usages of `ptr::null`.
- ///
- /// **Why is this bad?** This causes undefined behavior.
+ /// ### What it does
+ /// This lint checks for invalid usages of `ptr::null`.
///
- /// **Known problems:** None.
+ /// ### Why is this bad?
+ /// This causes undefined behavior.
///
- /// **Example:**
+ /// ### Example
/// ```ignore
/// // Bad. Undefined behavior
/// unsafe { std::slice::from_raw_parts(ptr::null(), 0); }
fn is_null_path(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
if let ExprKind::Call(pathexp, []) = expr.kind {
expr_path_res(cx, pathexp).opt_def_id().map_or(false, |id| {
- match_any_def_paths(cx, id, &[&paths::PTR_NULL, &paths::PTR_NULL_MUT]).is_some()
+ match_any_diagnostic_items(cx, id, &[sym::ptr_null, sym::ptr_null_mut]).is_some()
})
} else {
false