use clippy_utils::ty::has_drop;
use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res};
-use rustc_hir::{BinOpKind, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind, UnsafeSource};
+use rustc_hir::{is_range_literal, BinOpKind, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind, UnsafeSource};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use std::ops::Deref;
declare_clippy_lint! {
- /// **What it does:** Checks for statements which have no effect.
+ /// ### What it does
+ /// Checks for statements which have no effect.
///
- /// **Why is this bad?** Similar to dead code, these statements are actually
+ /// ### Why is this bad?
+ /// Similar to dead code, these statements are actually
/// executed. However, as they have no effect, all they do is make the code less
/// readable.
///
- /// **Known problems:** None.
- ///
- /// **Example:**
+ /// ### Example
/// ```rust
/// 0;
/// ```
}
declare_clippy_lint! {
- /// **What it does:** Checks for expression statements that can be reduced to a
+ /// ### What it does
+ /// Checks for expression statements that can be reduced to a
/// sub-expression.
///
- /// **Why is this bad?** Expressions by themselves often have no side-effects.
+ /// ### Why is this bad?
+ /// Expressions by themselves often have no side-effects.
/// Having such expressions reduces readability.
///
- /// **Known problems:** None.
- ///
- /// **Example:**
+ /// ### Example
/// ```rust,ignore
/// compute_array()[0];
/// ```
ExprKind::Call(callee, args) => {
if let ExprKind::Path(ref qpath) = callee.kind {
let res = cx.qpath_res(qpath, callee.hir_id);
- match res {
- Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..) => {
- !has_drop(cx, cx.typeck_results().expr_ty(expr))
- && args.iter().all(|arg| has_no_effect(cx, arg))
- },
- _ => false,
+ let def_matched = matches!(
+ res,
+ Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..)
+ );
+ if def_matched || is_range_literal(expr) {
+ !has_drop(cx, cx.typeck_results().expr_ty(expr)) && args.iter().all(|arg| has_no_effect(cx, arg))
+ } else {
+ false
}
} else {
false