-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #12310 - samueltardieu:issue-12307, r=xFrednet
New lint `const_is_empty` This lint detects calls to `.is_empty()` on an entity initialized from a string literal and flag them as suspicious. To avoid triggering on macros called from generated code, it checks that the `.is_empty()` receiver, the call itself and the initialization come from the same context. Fixes #12307 changelog: [`const_is_empty`]: new lint
- Loading branch information
Showing
18 changed files
with
566 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use clippy_utils::consts::constant_is_empty; | ||
use clippy_utils::diagnostics::span_lint; | ||
use clippy_utils::{find_binding_init, path_to_local}; | ||
use rustc_hir::{Expr, HirId}; | ||
use rustc_lint::{LateContext, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_span::sym; | ||
|
||
use super::CONST_IS_EMPTY; | ||
|
||
/// Expression whose initialization depend on a constant conditioned by a `#[cfg(…)]` directive will | ||
/// not trigger the lint. | ||
pub(super) fn check(cx: &LateContext<'_>, expr: &'_ Expr<'_>, receiver: &Expr<'_>) { | ||
if in_external_macro(cx.sess(), expr.span) || !receiver.span.eq_ctxt(expr.span) { | ||
return; | ||
} | ||
let init_expr = expr_or_init(cx, receiver); | ||
if !receiver.span.eq_ctxt(init_expr.span) { | ||
return; | ||
} | ||
if let Some(init_is_empty) = constant_is_empty(cx, init_expr) { | ||
span_lint( | ||
cx, | ||
CONST_IS_EMPTY, | ||
expr.span, | ||
&format!("this expression always evaluates to {init_is_empty:?}"), | ||
); | ||
} | ||
} | ||
|
||
fn is_under_cfg(cx: &LateContext<'_>, id: HirId) -> bool { | ||
cx.tcx | ||
.hir() | ||
.parent_id_iter(id) | ||
.any(|id| cx.tcx.hir().attrs(id).iter().any(|attr| attr.has_name(sym::cfg))) | ||
} | ||
|
||
/// Similar to [`clippy_utils::expr_or_init`], but does not go up the chain if the initialization | ||
/// value depends on a `#[cfg(…)]` directive. | ||
fn expr_or_init<'a, 'b, 'tcx: 'b>(cx: &LateContext<'tcx>, mut expr: &'a Expr<'b>) -> &'a Expr<'b> { | ||
while let Some(init) = path_to_local(expr) | ||
.and_then(|id| find_binding_init(cx, id)) | ||
.filter(|init| cx.typeck_results().expr_adjustments(init).is_empty()) | ||
.filter(|init| !is_under_cfg(cx, init.hir_id)) | ||
{ | ||
expr = init; | ||
} | ||
expr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.