Skip to content

Commit 1c9772c

Browse files
Move inherits_cfg function into clippy_utils
1 parent 04b710b commit 1c9772c

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

Diff for: clippy_lints/src/needless_pass_by_ref_mut.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::needless_pass_by_value::requires_exact_signature;
22
use clippy_utils::diagnostics::span_lint_hir_and_then;
33
use clippy_utils::source::snippet;
4-
use clippy_utils::{get_parent_node, is_from_proc_macro, is_self};
4+
use clippy_utils::{get_parent_node, inherits_cfg, is_from_proc_macro, is_self};
55
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
66
use rustc_errors::Applicability;
77
use rustc_hir::intravisit::{walk_qpath, FnKind, Visitor};
@@ -12,11 +12,11 @@ use rustc_lint::{LateContext, LateLintPass};
1212
use rustc_middle::hir::map::associated_body;
1313
use rustc_middle::hir::nested_filter::OnlyBodies;
1414
use rustc_middle::mir::FakeReadCause;
15-
use rustc_middle::ty::{self, Ty, TyCtxt, UpvarId, UpvarPath};
15+
use rustc_middle::ty::{self, Ty, UpvarId, UpvarPath};
1616
use rustc_session::{declare_tool_lint, impl_lint_pass};
17-
use rustc_span::def_id::{LocalDefId, CRATE_DEF_ID};
17+
use rustc_span::def_id::LocalDefId;
1818
use rustc_span::symbol::kw;
19-
use rustc_span::{sym, Span};
19+
use rustc_span::Span;
2020
use rustc_target::spec::abi::Abi;
2121

2222
declare_clippy_lint! {
@@ -93,16 +93,6 @@ fn should_skip<'tcx>(
9393
is_from_proc_macro(cx, &input)
9494
}
9595

96-
fn inherits_cfg(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
97-
if def_id == CRATE_DEF_ID {
98-
false
99-
} else if tcx.has_attr(def_id, sym::cfg) {
100-
true
101-
} else {
102-
inherits_cfg(tcx, tcx.parent_module_from_def_id(def_id))
103-
}
104-
}
105-
10696
impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut<'tcx> {
10797
fn check_fn(
10898
&mut self,

Diff for: clippy_utils/src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2451,6 +2451,17 @@ pub fn is_in_cfg_test(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
24512451
.any(is_cfg_test)
24522452
}
24532453

2454+
/// Checks if the item of any of its parents has `#[cfg(...)]` attribute applied.
2455+
pub fn inherits_cfg(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
2456+
let hir = tcx.hir();
2457+
2458+
tcx.has_attr(def_id, sym::cfg)
2459+
|| hir
2460+
.parent_iter(hir.local_def_id_to_hir_id(def_id))
2461+
.flat_map(|(parent_id, _)| hir.attrs(parent_id))
2462+
.any(|attr| attr.has_name(sym::cfg))
2463+
}
2464+
24542465
/// Checks whether item either has `test` attribute applied, or
24552466
/// is a module with `test` in its name.
24562467
///

0 commit comments

Comments
 (0)