Skip to content

Commit 4e7f974

Browse files
committed
Auto merge of #12827 - Alexendoo:lint-groups-priority, r=Jarcho
`lint_groups_priority`: ignore lints & groups at the same level Fixes #12270 changelog: none
2 parents 6cfd4ac + 9e5523e commit 4e7f974

File tree

4 files changed

+60
-60
lines changed

4 files changed

+60
-60
lines changed

clippy_lints/src/cargo/lint_groups_priority.rs

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@ struct CargoToml {
7171
workspace: Workspace,
7272
}
7373

74-
#[derive(Default, Debug)]
75-
struct LintsAndGroups {
76-
lints: Vec<Spanned<String>>,
77-
groups: Vec<(Spanned<String>, Spanned<LintConfig>)>,
78-
}
79-
8074
fn toml_span(range: Range<usize>, file: &SourceFile) -> Span {
8175
Span::new(
8276
file.start_pos + BytePos::from_usize(range.start),
@@ -86,27 +80,28 @@ fn toml_span(range: Range<usize>, file: &SourceFile) -> Span {
8680
)
8781
}
8882

89-
fn check_table(cx: &LateContext<'_>, table: LintTable, groups: &FxHashSet<&str>, file: &SourceFile) {
90-
let mut by_priority = BTreeMap::<_, LintsAndGroups>::new();
83+
fn check_table(cx: &LateContext<'_>, table: LintTable, known_groups: &FxHashSet<&str>, file: &SourceFile) {
84+
let mut lints = Vec::new();
85+
let mut groups = Vec::new();
9186
for (name, config) in table {
92-
let lints_and_groups = by_priority.entry(config.as_ref().priority()).or_default();
93-
if groups.contains(name.get_ref().as_str()) {
94-
lints_and_groups.groups.push((name, config));
87+
if name.get_ref() == "warnings" {
88+
continue;
89+
}
90+
91+
if known_groups.contains(name.get_ref().as_str()) {
92+
groups.push((name, config));
9593
} else {
96-
lints_and_groups.lints.push(name);
94+
lints.push((name, config.into_inner()));
9795
}
9896
}
99-
let low_priority = by_priority
100-
.iter()
101-
.find(|(_, lints_and_groups)| !lints_and_groups.lints.is_empty())
102-
.map_or(-1, |(&lowest_lint_priority, _)| lowest_lint_priority - 1);
10397

104-
for (priority, LintsAndGroups { lints, groups }) in by_priority {
105-
let Some(last_lint_alphabetically) = lints.last() else {
106-
continue;
107-
};
108-
109-
for (group, config) in groups {
98+
for (group, group_config) in groups {
99+
let priority = group_config.get_ref().priority();
100+
let level = group_config.get_ref().level();
101+
if let Some((conflict, _)) = lints
102+
.iter()
103+
.rfind(|(_, lint_config)| lint_config.priority() == priority && lint_config.level() != level)
104+
{
110105
span_lint_and_then(
111106
cx,
112107
LINT_GROUPS_PRIORITY,
@@ -116,22 +111,23 @@ fn check_table(cx: &LateContext<'_>, table: LintTable, groups: &FxHashSet<&str>,
116111
group.as_ref()
117112
),
118113
|diag| {
119-
let config_span = toml_span(config.span(), file);
120-
if config.as_ref().is_implicit() {
114+
let config_span = toml_span(group_config.span(), file);
115+
116+
if group_config.as_ref().is_implicit() {
121117
diag.span_label(config_span, "has an implicit priority of 0");
122118
}
123-
// add the label to next lint after this group that has the same priority
124-
let lint = lints
125-
.iter()
126-
.filter(|lint| lint.span().start > group.span().start)
127-
.min_by_key(|lint| lint.span().start)
128-
.unwrap_or(last_lint_alphabetically);
129-
diag.span_label(toml_span(lint.span(), file), "has the same priority as this lint");
119+
diag.span_label(toml_span(conflict.span(), file), "has the same priority as this lint");
130120
diag.note("the order of the lints in the table is ignored by Cargo");
121+
131122
let mut suggestion = String::new();
123+
let low_priority = lints
124+
.iter()
125+
.map(|(_, config)| config.priority().saturating_sub(1))
126+
.min()
127+
.unwrap_or(-1);
132128
Serialize::serialize(
133129
&LintConfigTable {
134-
level: config.as_ref().level().into(),
130+
level: level.into(),
135131
priority: Some(low_priority),
136132
},
137133
toml::ser::ValueSerializer::new(&mut suggestion),
Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
error: lint group `rust_2018_idioms` has the same priority (0) as a lint
2-
--> Cargo.toml:7:1
3-
|
4-
7 | rust_2018_idioms = "warn"
5-
| ^^^^^^^^^^^^^^^^ ------ has an implicit priority of 0
6-
8 | bare_trait_objects = "allow"
7-
| ------------------ has the same priority as this lint
8-
|
9-
= note: the order of the lints in the table is ignored by Cargo
10-
= note: `#[deny(clippy::lint_groups_priority)]` on by default
2+
--> Cargo.toml:7:1
3+
|
4+
7 | rust_2018_idioms = "warn"
5+
| ^^^^^^^^^^^^^^^^ ------ has an implicit priority of 0
6+
...
7+
12 | unused_attributes = { level = "allow" }
8+
| ----------------- has the same priority as this lint
9+
|
10+
= note: the order of the lints in the table is ignored by Cargo
11+
= note: `#[deny(clippy::lint_groups_priority)]` on by default
1112
help: to have lints override the group set `rust_2018_idioms` to a lower priority
12-
|
13-
7 | rust_2018_idioms = { level = "warn", priority = -1 }
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13+
|
14+
7 | rust_2018_idioms = { level = "warn", priority = -1 }
15+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1516

1617
error: lint group `unused` has the same priority (0) as a lint
1718
--> Cargo.toml:10:1
@@ -29,45 +30,45 @@ help: to have lints override the group set `unused` to a lower priority
2930
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3031

3132
error: lint group `pedantic` has the same priority (-1) as a lint
32-
--> Cargo.toml:19:1
33+
--> Cargo.toml:15:1
3334
|
34-
19 | pedantic = { level = "warn", priority = -1 }
35+
15 | pedantic = { level = "warn", priority = -1 }
3536
| ^^^^^^^^
36-
20 | similar_names = { level = "allow", priority = -1 }
37+
16 | similar_names = { level = "allow", priority = -1 }
3738
| ------------- has the same priority as this lint
3839
|
3940
= note: the order of the lints in the table is ignored by Cargo
4041
help: to have lints override the group set `pedantic` to a lower priority
4142
|
42-
19 | pedantic = { level = "warn", priority = -2 }
43+
15 | pedantic = { level = "warn", priority = -2 }
4344
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4445

4546
error: lint group `rust_2018_idioms` has the same priority (0) as a lint
46-
--> Cargo.toml:23:1
47+
--> Cargo.toml:19:1
4748
|
48-
23 | rust_2018_idioms = "warn"
49+
19 | rust_2018_idioms = "warn"
4950
| ^^^^^^^^^^^^^^^^ ------ has an implicit priority of 0
50-
24 | bare_trait_objects = "allow"
51+
20 | bare_trait_objects = "allow"
5152
| ------------------ has the same priority as this lint
5253
|
5354
= note: the order of the lints in the table is ignored by Cargo
5455
help: to have lints override the group set `rust_2018_idioms` to a lower priority
5556
|
56-
23 | rust_2018_idioms = { level = "warn", priority = -1 }
57+
19 | rust_2018_idioms = { level = "warn", priority = -1 }
5758
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5859

5960
error: lint group `pedantic` has the same priority (0) as a lint
60-
--> Cargo.toml:27:1
61+
--> Cargo.toml:23:1
6162
|
62-
27 | pedantic = "warn"
63+
23 | pedantic = "warn"
6364
| ^^^^^^^^ ------ has an implicit priority of 0
64-
28 | similar_names = "allow"
65+
24 | similar_names = "allow"
6566
| ------------- has the same priority as this lint
6667
|
6768
= note: the order of the lints in the table is ignored by Cargo
6869
help: to have lints override the group set `pedantic` to a lower priority
6970
|
70-
27 | pedantic = { level = "warn", priority = -1 }
71+
23 | pedantic = { level = "warn", priority = -1 }
7172
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7273

7374
error: could not compile `fail` (lib) due to 5 previous errors

tests/ui-cargo/lint_groups_priority/fail/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ unused = { level = "deny" }
1111
unused_braces = { level = "allow", priority = 1 }
1212
unused_attributes = { level = "allow" }
1313

14-
# `warnings` is not a group so the order it is passed does not matter
15-
warnings = "deny"
16-
deprecated = "allow"
17-
1814
[lints.clippy]
1915
pedantic = { level = "warn", priority = -1 }
2016
similar_names = { level = "allow", priority = -1 }

tests/ui-cargo/lint_groups_priority/pass/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ name = "pass"
33
version = "0.1.0"
44
publish = false
55

6+
[lints.rust]
7+
# Warnings does not conflict with any group or lint
8+
warnings = "deny"
9+
# Groups & lints at the same level do not conflict
10+
rust_2018_idioms = "warn"
11+
unsafe_code = "warn"
12+
613
[lints.clippy]
714
pedantic = { level = "warn", priority = -1 }
815
style = { level = "warn", priority = 1 }

0 commit comments

Comments
 (0)