Skip to content

Commit 8d427b6

Browse files
committed
Auto merge of rust-lang#7379 - popzxc:issue-7305, r=flip1995
Do not spawn blacklisted_name lint in test context --- fixed rust-lang#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.
2 parents a03dfb9 + 28d3873 commit 8d427b6

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

clippy_lints/src/blacklisted_name.rs

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use clippy_utils::diagnostics::span_lint;
1+
use clippy_utils::{diagnostics::span_lint, is_test_module_or_function};
22
use rustc_data_structures::fx::FxHashSet;
3-
use rustc_hir::{Pat, PatKind};
3+
use rustc_hir::{Item, Pat, PatKind};
44
use rustc_lint::{LateContext, LateLintPass};
55
use rustc_session::{declare_tool_lint, impl_lint_pass};
66

@@ -25,18 +25,37 @@ declare_clippy_lint! {
2525
#[derive(Clone, Debug)]
2626
pub struct BlacklistedName {
2727
blacklist: FxHashSet<String>,
28+
test_modules_deep: u32,
2829
}
2930

3031
impl BlacklistedName {
3132
pub fn new(blacklist: FxHashSet<String>) -> Self {
32-
Self { blacklist }
33+
Self {
34+
blacklist,
35+
test_modules_deep: 0,
36+
}
37+
}
38+
39+
fn in_test_module(&self) -> bool {
40+
self.test_modules_deep != 0
3341
}
3442
}
3543

3644
impl_lint_pass!(BlacklistedName => [BLACKLISTED_NAME]);
3745

3846
impl<'tcx> LateLintPass<'tcx> for BlacklistedName {
47+
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
48+
if is_test_module_or_function(cx.tcx, item) {
49+
self.test_modules_deep = self.test_modules_deep.saturating_add(1);
50+
}
51+
}
52+
3953
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
54+
// Check whether we are under the `test` attribute.
55+
if self.in_test_module() {
56+
return;
57+
}
58+
4059
if let PatKind::Binding(.., ident, _) = pat.kind {
4160
if self.blacklist.contains(&ident.name.to_string()) {
4261
span_lint(
@@ -48,4 +67,10 @@ impl<'tcx> LateLintPass<'tcx> for BlacklistedName {
4867
}
4968
}
5069
}
70+
71+
fn check_item_post(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
72+
if is_test_module_or_function(cx.tcx, item) {
73+
self.test_modules_deep = self.test_modules_deep.saturating_sub(1);
74+
}
75+
}
5176
}

clippy_lints/src/wildcard_imports.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::in_macro;
32
use clippy_utils::source::{snippet, snippet_with_applicability};
3+
use clippy_utils::{in_macro, is_test_module_or_function};
44
use if_chain::if_chain;
55
use rustc_errors::Applicability;
66
use rustc_hir::{
@@ -106,7 +106,7 @@ impl_lint_pass!(WildcardImports => [ENUM_GLOB_USE, WILDCARD_IMPORTS]);
106106

107107
impl LateLintPass<'_> for WildcardImports {
108108
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
109-
if is_test_module_or_function(item) {
109+
if is_test_module_or_function(cx.tcx, item) {
110110
self.test_modules_deep = self.test_modules_deep.saturating_add(1);
111111
}
112112
if item.vis.node.is_pub() || item.vis.node.is_pub_restricted() {
@@ -183,8 +183,8 @@ impl LateLintPass<'_> for WildcardImports {
183183
}
184184
}
185185

186-
fn check_item_post(&mut self, _: &LateContext<'_>, item: &Item<'_>) {
187-
if is_test_module_or_function(item) {
186+
fn check_item_post(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
187+
if is_test_module_or_function(cx.tcx, item) {
188188
self.test_modules_deep = self.test_modules_deep.saturating_sub(1);
189189
}
190190
}
@@ -208,7 +208,3 @@ fn is_prelude_import(segments: &[PathSegment<'_>]) -> bool {
208208
fn is_super_only_import(segments: &[PathSegment<'_>]) -> bool {
209209
segments.len() == 1 && segments[0].ident.name == kw::Super
210210
}
211-
212-
fn is_test_module_or_function(item: &Item<'_>) -> bool {
213-
matches!(item.kind, ItemKind::Mod(..)) && item.ident.name.as_str().contains("test")
214-
}

clippy_utils/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1727,3 +1727,15 @@ pub fn is_hir_ty_cfg_dependant(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> bool {
17271727
}
17281728
}
17291729
}
1730+
1731+
/// Checks whether item either has `test` attribute applied, or
1732+
/// is a module with `test` in its name.
1733+
pub fn is_test_module_or_function(tcx: TyCtxt<'_>, item: &Item<'_>) -> bool {
1734+
if let Some(def_id) = tcx.hir().opt_local_def_id(item.hir_id()) {
1735+
if tcx.has_attr(def_id.to_def_id(), sym::test) {
1736+
return true;
1737+
}
1738+
}
1739+
1740+
matches!(item.kind, ItemKind::Mod(..)) && item.ident.name.as_str().contains("test")
1741+
}

tests/ui/blacklisted_name.rs

+12
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,15 @@ fn issue_1647_ref_mut() {
4343
let ref mut baz = 0;
4444
if let Some(ref mut quux) = Some(42) {}
4545
}
46+
47+
mod tests {
48+
fn issue_7305() {
49+
// `blackisted_name` lint should not be triggered inside of the test code.
50+
let foo = 0;
51+
52+
// Check that even in nested functions warning is still not triggere.
53+
fn nested() {
54+
let foo = 0;
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)