X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Fexhaustive_items.rs;h=60ad2e8ee1404fec06b5b71a5b9bc68c96a11b02;hb=b094bb1bd7f1cc702823c91ca509f338fedee24a;hp=32b1299efce91d1fe44fdbac532f1ecf52dbd3c0;hpb=9607b5c6ac2d6b3d2117c8d5868435581c5e67d1;p=rust.git diff --git a/clippy_lints/src/exhaustive_items.rs b/clippy_lints/src/exhaustive_items.rs index 32b1299efce..60ad2e8ee14 100644 --- a/clippy_lints/src/exhaustive_items.rs +++ b/clippy_lints/src/exhaustive_items.rs @@ -1,4 +1,5 @@ -use crate::utils::{indent_of, span_lint_and_then}; +use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::source::indent_of; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::{Item, ItemKind}; @@ -72,13 +73,18 @@ impl LateLintPass<'_> for ExhaustiveItems { fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { if_chain! { if let ItemKind::Enum(..) | ItemKind::Struct(..) = item.kind; - if cx.access_levels.is_exported(item.hir_id); - if !item.attrs.iter().any(|a| a.has_name(sym::non_exhaustive)); + if cx.access_levels.is_exported(item.hir_id()); + let attrs = cx.tcx.hir().attrs(item.hir_id()); + if !attrs.iter().any(|a| a.has_name(sym::non_exhaustive)); then { - let (lint, msg) = if let ItemKind::Enum(..) = item.kind { - (EXHAUSTIVE_ENUMS, "exported enums should not be exhaustive") - } else { + let (lint, msg) = if let ItemKind::Struct(ref v, ..) = item.kind { + if v.fields().iter().any(|f| !f.vis.node.is_pub()) { + // skip structs with private fields + return; + } (EXHAUSTIVE_STRUCTS, "exported structs should not be exhaustive") + } else { + (EXHAUSTIVE_ENUMS, "exported enums should not be exhaustive") }; let suggestion_span = item.span.shrink_to_lo(); let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0));