diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2023-07-25 18:43:51 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2023-07-25 18:43:58 +0200 |
| commit | 1c9772c7737e5937f71be1d1a27699bf226d89f1 (patch) | |
| tree | 929f977bf58e0df6f60e9f5f354690d7fdf61529 | |
| parent | 04b710b5695a80b7265c92b1ff7fdaf4be81efa6 (diff) | |
| download | rust-1c9772c7737e5937f71be1d1a27699bf226d89f1.tar.gz rust-1c9772c7737e5937f71be1d1a27699bf226d89f1.zip | |
Move `inherits_cfg` function into clippy_utils
| -rw-r--r-- | clippy_lints/src/needless_pass_by_ref_mut.rs | 18 | ||||
| -rw-r--r-- | clippy_utils/src/lib.rs | 11 |
2 files changed, 15 insertions, 14 deletions
diff --git a/clippy_lints/src/needless_pass_by_ref_mut.rs b/clippy_lints/src/needless_pass_by_ref_mut.rs index b4be910c8ad..0e2a101a977 100644 --- a/clippy_lints/src/needless_pass_by_ref_mut.rs +++ b/clippy_lints/src/needless_pass_by_ref_mut.rs @@ -1,7 +1,7 @@ use super::needless_pass_by_value::requires_exact_signature; use clippy_utils::diagnostics::span_lint_hir_and_then; use clippy_utils::source::snippet; -use clippy_utils::{get_parent_node, is_from_proc_macro, is_self}; +use clippy_utils::{get_parent_node, inherits_cfg, is_from_proc_macro, is_self}; use rustc_data_structures::fx::{FxHashSet, FxIndexMap}; use rustc_errors::Applicability; use rustc_hir::intravisit::{walk_qpath, FnKind, Visitor}; @@ -12,11 +12,11 @@ use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::map::associated_body; use rustc_middle::hir::nested_filter::OnlyBodies; use rustc_middle::mir::FakeReadCause; -use rustc_middle::ty::{self, Ty, TyCtxt, UpvarId, UpvarPath}; +use rustc_middle::ty::{self, Ty, UpvarId, UpvarPath}; use rustc_session::{declare_tool_lint, impl_lint_pass}; -use rustc_span::def_id::{LocalDefId, CRATE_DEF_ID}; +use rustc_span::def_id::LocalDefId; use rustc_span::symbol::kw; -use rustc_span::{sym, Span}; +use rustc_span::Span; use rustc_target::spec::abi::Abi; declare_clippy_lint! { @@ -93,16 +93,6 @@ fn should_skip<'tcx>( is_from_proc_macro(cx, &input) } -fn inherits_cfg(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { - if def_id == CRATE_DEF_ID { - false - } else if tcx.has_attr(def_id, sym::cfg) { - true - } else { - inherits_cfg(tcx, tcx.parent_module_from_def_id(def_id)) - } -} - impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut<'tcx> { fn check_fn( &mut self, diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 8429192437c..beff0e6e1ab 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -2451,6 +2451,17 @@ pub fn is_in_cfg_test(tcx: TyCtxt<'_>, id: hir::HirId) -> bool { .any(is_cfg_test) } +/// Checks if the item of any of its parents has `#[cfg(...)]` attribute applied. +pub fn inherits_cfg(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { + let hir = tcx.hir(); + + tcx.has_attr(def_id, sym::cfg) + || hir + .parent_iter(hir.local_def_id_to_hir_id(def_id)) + .flat_map(|(parent_id, _)| hir.attrs(parent_id)) + .any(|attr| attr.has_name(sym::cfg)) +} + /// Checks whether item either has `test` attribute applied, or /// is a module with `test` in its name. /// |
