Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2423,16 +2423,27 @@ pub fn is_test_function(tcx: TyCtxt<'_>, fn_def_id: LocalDefId) -> bool {
/// use [`is_in_cfg_test`]
pub fn is_cfg_test(tcx: TyCtxt<'_>, id: HirId) -> bool {
if let Some(cfgs) = find_attr!(tcx.hir_attrs(id), CfgTrace(cfgs) => cfgs)
&& cfgs
.iter()
.any(|(cfg, _)| matches!(cfg, CfgEntry::NameValue { name: sym::test, .. }))
&& cfgs.iter().any(|(cfg, _)| cfg_entry_requires_test(cfg))
{
true
} else {
false
}
}

/// Checks whether a `CfgEntry` requires `test` to be true.
///
/// Returns `true` for `test`, `all(test, ...)`, and nested combinations
/// where `test` must hold. Returns `false` for `any(test, ...)` (since
/// the condition can be satisfied without `test`) and `not(test)`.
fn cfg_entry_requires_test(cfg: &CfgEntry) -> bool {
match cfg {
CfgEntry::NameValue { name, .. } => *name == sym::test,
CfgEntry::All(entries, _) => entries.iter().any(cfg_entry_requires_test),
_ => false,
}
}

/// Checks if any parent node of `HirId` has `#[cfg(test)]` attribute applied
pub fn is_in_cfg_test(tcx: TyCtxt<'_>, id: HirId) -> bool {
tcx.hir_parent_id_iter(id).any(|parent_id| is_cfg_test(tcx, parent_id))
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/tests_outside_test_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,18 @@ mod tests {
#[test]
fn my_test() {}
}

#[allow(clippy::non_minimal_cfg)]
#[cfg(all(test))]
mod tests_all {
// Should not lint: `all(test)` implies `test`
#[test]
fn my_test() {}
}

#[cfg(all(test, not(target_pointer_width = "16")))]
mod tests_all_compound {
// Should not lint: `all(test, ...)` implies `test`
#[test]
fn my_test() {}
}