2019-11-16 08:55:00 -06:00
|
|
|
use if_chain::if_chain;
|
2020-04-01 14:18:16 -05:00
|
|
|
use rustc_hir::{Local, PatKind};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-03-30 04:02:14 -05:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
2020-04-07 14:53:56 -05:00
|
|
|
use rustc_middle::ty::subst::GenericArgKind;
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2019-11-16 08:55:00 -06:00
|
|
|
|
2020-11-07 17:26:14 -06:00
|
|
|
use crate::utils::{implements_trait, is_must_use_func_call, is_must_use_ty, match_type, paths, span_lint_and_help};
|
2019-11-16 08:55:00 -06:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for `let _ = <expr>`
|
|
|
|
/// where expr is #[must_use]
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** It's better to explicitly
|
|
|
|
/// handle the value of a #[must_use] expr
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// fn f() -> Result<u32, u32> {
|
|
|
|
/// Ok(0)
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let _ = f();
|
|
|
|
/// // is_ok() is marked #[must_use]
|
|
|
|
/// let _ = f().is_ok();
|
|
|
|
/// ```
|
|
|
|
pub LET_UNDERSCORE_MUST_USE,
|
|
|
|
restriction,
|
2020-01-06 00:30:43 -06:00
|
|
|
"non-binding let on a `#[must_use]` expression"
|
2019-11-16 08:55:00 -06:00
|
|
|
}
|
|
|
|
|
2020-01-27 08:34:30 -06:00
|
|
|
declare_clippy_lint! {
|
2020-01-30 09:10:19 -06:00
|
|
|
/// **What it does:** Checks for `let _ = sync_lock`
|
2020-01-27 08:34:30 -06:00
|
|
|
///
|
2020-01-30 09:10:19 -06:00
|
|
|
/// **Why is this bad?** This statement immediately drops the lock instead of
|
2020-06-23 10:05:22 -05:00
|
|
|
/// extending its lifetime to the end of the scope, which is often not intended.
|
2020-01-30 09:10:19 -06:00
|
|
|
/// To extend lock lifetime to the end of the scope, use an underscore-prefixed
|
|
|
|
/// name instead (i.e. _lock). If you want to explicitly drop the lock,
|
|
|
|
/// `std::mem::drop` conveys your intention better and is less error-prone.
|
2020-01-27 08:34:30 -06:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// Bad:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// let _ = mutex.lock();
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Good:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// let _lock = mutex.lock();
|
|
|
|
/// ```
|
|
|
|
pub LET_UNDERSCORE_LOCK,
|
|
|
|
correctness,
|
|
|
|
"non-binding let on a synchronization lock"
|
|
|
|
}
|
|
|
|
|
2020-11-07 17:26:14 -06:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for `let _ = <expr>`
|
|
|
|
/// where expr has a type that implements `Drop`
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** This statement immediately drops the initializer
|
|
|
|
/// expression instead of extending its lifetime to the end of the scope, which
|
|
|
|
/// is often not intended. To extend the expression's lifetime to the end of the
|
|
|
|
/// scope, use an underscore-prefixed name instead (i.e. _var). If you want to
|
|
|
|
/// explicitly drop the expression, `std::mem::drop` conveys your intention
|
|
|
|
/// better and is less error-prone.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// Bad:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// struct Droppable;
|
|
|
|
/// impl Drop for Droppable {
|
|
|
|
/// fn drop(&mut self) {}
|
|
|
|
/// }
|
2020-11-08 16:25:23 -06:00
|
|
|
/// {
|
|
|
|
/// let _ = Droppable;
|
|
|
|
/// // ^ dropped here
|
|
|
|
/// /* more code */
|
|
|
|
/// }
|
2020-11-07 17:26:14 -06:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Good:
|
|
|
|
/// ```rust,ignore
|
2020-11-08 16:25:23 -06:00
|
|
|
/// {
|
|
|
|
/// let _droppable = Droppable;
|
|
|
|
/// /* more code */
|
|
|
|
/// // dropped at end of scope
|
|
|
|
/// }
|
2020-11-07 17:26:14 -06:00
|
|
|
/// ```
|
|
|
|
pub LET_UNDERSCORE_DROP,
|
2020-11-08 16:25:50 -06:00
|
|
|
pedantic,
|
2020-11-07 17:26:14 -06:00
|
|
|
"non-binding let on a type that implements `Drop`"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_DROP]);
|
2020-01-27 08:34:30 -06:00
|
|
|
|
2020-01-30 09:10:19 -06:00
|
|
|
const SYNC_GUARD_PATHS: [&[&str]; 3] = [
|
|
|
|
&paths::MUTEX_GUARD,
|
|
|
|
&paths::RWLOCK_READ_GUARD,
|
|
|
|
&paths::RWLOCK_WRITE_GUARD,
|
|
|
|
];
|
2019-11-16 08:55:00 -06:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
|
|
|
|
fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
|
2020-04-01 14:18:16 -05:00
|
|
|
if in_external_macro(cx.tcx.sess, local.span) {
|
2020-01-23 09:20:50 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-16 08:55:00 -06:00
|
|
|
if_chain! {
|
|
|
|
if let PatKind::Wild = local.pat.kind;
|
|
|
|
if let Some(ref init) = local.init;
|
|
|
|
then {
|
2020-07-17 03:47:04 -05:00
|
|
|
let init_ty = cx.typeck_results().expr_ty(init);
|
2020-04-07 14:53:56 -05:00
|
|
|
let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() {
|
2020-04-07 15:19:20 -05:00
|
|
|
GenericArgKind::Type(inner_ty) => {
|
|
|
|
SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
|
|
|
|
},
|
2020-04-07 14:53:56 -05:00
|
|
|
|
|
|
|
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
|
|
|
|
});
|
2020-11-07 17:26:14 -06:00
|
|
|
let implements_drop = cx.tcx.lang_items().drop_trait().map_or(false, |drop_trait|
|
|
|
|
init_ty.walk().any(|inner| match inner.unpack() {
|
|
|
|
GenericArgKind::Type(inner_ty) => {
|
|
|
|
implements_trait(cx, inner_ty, drop_trait, &[])
|
|
|
|
},
|
|
|
|
|
|
|
|
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
|
|
|
|
})
|
|
|
|
);
|
2020-04-07 14:53:56 -05:00
|
|
|
if contains_sync_guard {
|
2020-01-30 09:10:19 -06:00
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
LET_UNDERSCORE_LOCK,
|
2020-04-01 14:18:16 -05:00
|
|
|
local.span,
|
2020-01-30 09:10:19 -06:00
|
|
|
"non-binding let on a synchronization lock",
|
2020-04-18 05:28:29 -05:00
|
|
|
None,
|
2020-01-30 09:10:19 -06:00
|
|
|
"consider using an underscore-prefixed named \
|
2020-11-07 17:26:14 -06:00
|
|
|
binding or dropping explicitly with `std::mem::drop`"
|
|
|
|
)
|
|
|
|
} else if implements_drop {
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
LET_UNDERSCORE_DROP,
|
|
|
|
local.span,
|
2020-11-08 16:24:55 -06:00
|
|
|
"non-binding `let` on a type that implements `Drop`",
|
2020-11-07 17:26:14 -06:00
|
|
|
None,
|
|
|
|
"consider using an underscore-prefixed named \
|
2020-01-30 09:10:19 -06:00
|
|
|
binding or dropping explicitly with `std::mem::drop`"
|
|
|
|
)
|
2020-07-17 03:47:04 -05:00
|
|
|
} else if is_must_use_ty(cx, cx.typeck_results().expr_ty(init)) {
|
2020-01-30 09:10:19 -06:00
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
LET_UNDERSCORE_MUST_USE,
|
2020-04-01 14:18:16 -05:00
|
|
|
local.span,
|
2020-01-30 09:10:19 -06:00
|
|
|
"non-binding let on an expression with `#[must_use]` type",
|
2020-04-18 05:28:29 -05:00
|
|
|
None,
|
2020-01-30 09:10:19 -06:00
|
|
|
"consider explicitly using expression value"
|
|
|
|
)
|
|
|
|
} else if is_must_use_func_call(cx, init) {
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
LET_UNDERSCORE_MUST_USE,
|
2020-04-01 14:18:16 -05:00
|
|
|
local.span,
|
2020-01-30 09:10:19 -06:00
|
|
|
"non-binding let on a result of a `#[must_use]` function",
|
2020-04-18 05:28:29 -05:00
|
|
|
None,
|
2020-01-30 09:10:19 -06:00
|
|
|
"consider explicitly using function result"
|
|
|
|
)
|
2019-11-16 08:55:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|