Auto merge of #7379 - popzxc:issue-7305, r=flip1995
Do not spawn blacklisted_name lint in test context --- fixed #7305 *Please write a short comment explaining your change (or "none" for internal only changes)* changelog: `blacklisted_name` lint is not spawned in the test context anymore.
This commit is contained in:
commit
8d427b624f
@ -1,6 +1,6 @@
|
||||
use clippy_utils::diagnostics::span_lint;
|
||||
use clippy_utils::{diagnostics::span_lint, is_test_module_or_function};
|
||||
use rustc_data_structures::fx::FxHashSet;
|
||||
use rustc_hir::{Pat, PatKind};
|
||||
use rustc_hir::{Item, Pat, PatKind};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||
|
||||
@ -25,18 +25,37 @@
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct BlacklistedName {
|
||||
blacklist: FxHashSet<String>,
|
||||
test_modules_deep: u32,
|
||||
}
|
||||
|
||||
impl BlacklistedName {
|
||||
pub fn new(blacklist: FxHashSet<String>) -> Self {
|
||||
Self { blacklist }
|
||||
Self {
|
||||
blacklist,
|
||||
test_modules_deep: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn in_test_module(&self) -> bool {
|
||||
self.test_modules_deep != 0
|
||||
}
|
||||
}
|
||||
|
||||
impl_lint_pass!(BlacklistedName => [BLACKLISTED_NAME]);
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for BlacklistedName {
|
||||
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
|
||||
if is_test_module_or_function(cx.tcx, item) {
|
||||
self.test_modules_deep = self.test_modules_deep.saturating_add(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
|
||||
// Check whether we are under the `test` attribute.
|
||||
if self.in_test_module() {
|
||||
return;
|
||||
}
|
||||
|
||||
if let PatKind::Binding(.., ident, _) = pat.kind {
|
||||
if self.blacklist.contains(&ident.name.to_string()) {
|
||||
span_lint(
|
||||
@ -48,4 +67,10 @@ fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_item_post(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
|
||||
if is_test_module_or_function(cx.tcx, item) {
|
||||
self.test_modules_deep = self.test_modules_deep.saturating_sub(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::in_macro;
|
||||
use clippy_utils::source::{snippet, snippet_with_applicability};
|
||||
use clippy_utils::{in_macro, is_test_module_or_function};
|
||||
use if_chain::if_chain;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{
|
||||
@ -106,7 +106,7 @@ pub fn new(warn_on_all: bool) -> Self {
|
||||
|
||||
impl LateLintPass<'_> for WildcardImports {
|
||||
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
|
||||
if is_test_module_or_function(item) {
|
||||
if is_test_module_or_function(cx.tcx, item) {
|
||||
self.test_modules_deep = self.test_modules_deep.saturating_add(1);
|
||||
}
|
||||
if item.vis.node.is_pub() || item.vis.node.is_pub_restricted() {
|
||||
@ -183,8 +183,8 @@ fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
|
||||
}
|
||||
}
|
||||
|
||||
fn check_item_post(&mut self, _: &LateContext<'_>, item: &Item<'_>) {
|
||||
if is_test_module_or_function(item) {
|
||||
fn check_item_post(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
|
||||
if is_test_module_or_function(cx.tcx, item) {
|
||||
self.test_modules_deep = self.test_modules_deep.saturating_sub(1);
|
||||
}
|
||||
}
|
||||
@ -208,7 +208,3 @@ fn is_prelude_import(segments: &[PathSegment<'_>]) -> bool {
|
||||
fn is_super_only_import(segments: &[PathSegment<'_>]) -> bool {
|
||||
segments.len() == 1 && segments[0].ident.name == kw::Super
|
||||
}
|
||||
|
||||
fn is_test_module_or_function(item: &Item<'_>) -> bool {
|
||||
matches!(item.kind, ItemKind::Mod(..)) && item.ident.name.as_str().contains("test")
|
||||
}
|
||||
|
@ -1727,3 +1727,15 @@ pub fn is_hir_ty_cfg_dependant(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> bool {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks whether item either has `test` attribute applied, or
|
||||
/// is a module with `test` in its name.
|
||||
pub fn is_test_module_or_function(tcx: TyCtxt<'_>, item: &Item<'_>) -> bool {
|
||||
if let Some(def_id) = tcx.hir().opt_local_def_id(item.hir_id()) {
|
||||
if tcx.has_attr(def_id.to_def_id(), sym::test) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
matches!(item.kind, ItemKind::Mod(..)) && item.ident.name.as_str().contains("test")
|
||||
}
|
||||
|
@ -43,3 +43,15 @@ fn issue_1647_ref_mut() {
|
||||
let ref mut baz = 0;
|
||||
if let Some(ref mut quux) = Some(42) {}
|
||||
}
|
||||
|
||||
mod tests {
|
||||
fn issue_7305() {
|
||||
// `blackisted_name` lint should not be triggered inside of the test code.
|
||||
let foo = 0;
|
||||
|
||||
// Check that even in nested functions warning is still not triggere.
|
||||
fn nested() {
|
||||
let foo = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user