exhaustive_structs: don't trigger for structs with private fields
This commit is contained in:
parent
c5f3f9df3b
commit
0fb09d6b21
4 changed files with 42 additions and 20 deletions
|
@ -75,10 +75,14 @@ impl LateLintPass<'_> for ExhaustiveItems {
|
||||||
if cx.access_levels.is_exported(item.hir_id);
|
if cx.access_levels.is_exported(item.hir_id);
|
||||||
if !item.attrs.iter().any(|a| a.has_name(sym::non_exhaustive));
|
if !item.attrs.iter().any(|a| a.has_name(sym::non_exhaustive));
|
||||||
then {
|
then {
|
||||||
let (lint, msg) = if let ItemKind::Enum(..) = item.kind {
|
let (lint, msg) = if let ItemKind::Struct(ref v, ..) = item.kind {
|
||||||
(EXHAUSTIVE_ENUMS, "exported enums should not be exhaustive")
|
if v.fields().iter().any(|f| !f.vis.node.is_pub()) {
|
||||||
} else {
|
// skip structs with private fields
|
||||||
|
return;
|
||||||
|
}
|
||||||
(EXHAUSTIVE_STRUCTS, "exported structs should not be exhaustive")
|
(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 suggestion_span = item.span.shrink_to_lo();
|
||||||
let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0));
|
let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0));
|
||||||
|
|
|
@ -56,27 +56,36 @@ pub mod enums {
|
||||||
pub mod structs {
|
pub mod structs {
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub struct Exhaustive {
|
pub struct Exhaustive {
|
||||||
foo: u8,
|
pub foo: u8,
|
||||||
bar: String,
|
pub bar: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// no warning, already non_exhaustive
|
// no warning, already non_exhaustive
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub struct NonExhaustive {
|
pub struct NonExhaustive {
|
||||||
foo: u8,
|
pub foo: u8,
|
||||||
bar: String,
|
pub bar: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
// no warning, private fields
|
||||||
|
pub struct ExhaustivePrivateFieldTuple(u8);
|
||||||
|
|
||||||
|
// no warning, private fields
|
||||||
|
pub struct ExhaustivePrivateField {
|
||||||
|
pub foo: u8,
|
||||||
|
bar: String
|
||||||
}
|
}
|
||||||
|
|
||||||
// no warning, private
|
// no warning, private
|
||||||
struct ExhaustivePrivate {
|
struct ExhaustivePrivate {
|
||||||
foo: u8,
|
pub foo: u8,
|
||||||
bar: String,
|
pub bar: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// no warning, private
|
// no warning, private
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
struct NonExhaustivePrivate {
|
struct NonExhaustivePrivate {
|
||||||
foo: u8,
|
pub foo: u8,
|
||||||
bar: String,
|
pub bar: String,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,27 +53,36 @@ pub mod enums {
|
||||||
|
|
||||||
pub mod structs {
|
pub mod structs {
|
||||||
pub struct Exhaustive {
|
pub struct Exhaustive {
|
||||||
foo: u8,
|
pub foo: u8,
|
||||||
bar: String,
|
pub bar: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// no warning, already non_exhaustive
|
// no warning, already non_exhaustive
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub struct NonExhaustive {
|
pub struct NonExhaustive {
|
||||||
foo: u8,
|
pub foo: u8,
|
||||||
|
pub bar: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
// no warning, private fields
|
||||||
|
pub struct ExhaustivePrivateFieldTuple(u8);
|
||||||
|
|
||||||
|
// no warning, private fields
|
||||||
|
pub struct ExhaustivePrivateField {
|
||||||
|
pub foo: u8,
|
||||||
bar: String,
|
bar: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// no warning, private
|
// no warning, private
|
||||||
struct ExhaustivePrivate {
|
struct ExhaustivePrivate {
|
||||||
foo: u8,
|
pub foo: u8,
|
||||||
bar: String,
|
pub bar: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// no warning, private
|
// no warning, private
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
struct NonExhaustivePrivate {
|
struct NonExhaustivePrivate {
|
||||||
foo: u8,
|
pub foo: u8,
|
||||||
bar: String,
|
pub bar: String,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,8 +41,8 @@ error: exported structs should not be exhaustive
|
||||||
--> $DIR/exhaustive_items.rs:55:5
|
--> $DIR/exhaustive_items.rs:55:5
|
||||||
|
|
|
|
||||||
LL | / pub struct Exhaustive {
|
LL | / pub struct Exhaustive {
|
||||||
LL | | foo: u8,
|
LL | | pub foo: u8,
|
||||||
LL | | bar: String,
|
LL | | pub bar: String,
|
||||||
LL | | }
|
LL | | }
|
||||||
| |_____^
|
| |_____^
|
||||||
|
|
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue