|
| 1 | +use super::LINT_GROUPS_PRIORITY; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 3 | +use rustc_data_structures::fx::FxHashSet; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_lint::{unerased_lint_store, LateContext}; |
| 6 | +use rustc_span::{BytePos, Pos, SourceFile, Span, SyntaxContext}; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | +use std::collections::BTreeMap; |
| 9 | +use std::ops::Range; |
| 10 | +use std::path::Path; |
| 11 | +use toml::Spanned; |
| 12 | + |
| 13 | +#[derive(Deserialize, Serialize, Debug)] |
| 14 | +struct LintConfigTable { |
| 15 | + level: String, |
| 16 | + priority: Option<i64>, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Deserialize, Debug)] |
| 20 | +#[serde(untagged)] |
| 21 | +enum LintConfig { |
| 22 | + Level(String), |
| 23 | + Table(LintConfigTable), |
| 24 | +} |
| 25 | + |
| 26 | +impl LintConfig { |
| 27 | + fn level(&self) -> &str { |
| 28 | + match self { |
| 29 | + LintConfig::Level(level) => level, |
| 30 | + LintConfig::Table(table) => &table.level, |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + fn priority(&self) -> i64 { |
| 35 | + match self { |
| 36 | + LintConfig::Level(_) => 0, |
| 37 | + LintConfig::Table(table) => table.priority.unwrap_or(0), |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + fn is_implicit(&self) -> bool { |
| 42 | + if let LintConfig::Table(table) = self { |
| 43 | + table.priority.is_none() |
| 44 | + } else { |
| 45 | + true |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +type LintTable = BTreeMap<Spanned<String>, Spanned<LintConfig>>; |
| 51 | + |
| 52 | +#[derive(Deserialize, Debug)] |
| 53 | +struct Lints { |
| 54 | + #[serde(default)] |
| 55 | + rust: LintTable, |
| 56 | + #[serde(default)] |
| 57 | + clippy: LintTable, |
| 58 | +} |
| 59 | + |
| 60 | +#[derive(Deserialize, Debug)] |
| 61 | +struct CargoToml { |
| 62 | + lints: Lints, |
| 63 | +} |
| 64 | + |
| 65 | +#[derive(Default, Debug)] |
| 66 | +struct LintsAndGroups { |
| 67 | + lints: Vec<Spanned<String>>, |
| 68 | + groups: Vec<(Spanned<String>, Spanned<LintConfig>)>, |
| 69 | +} |
| 70 | + |
| 71 | +fn toml_span(range: Range<usize>, file: &SourceFile) -> Span { |
| 72 | + Span::new( |
| 73 | + file.start_pos + BytePos::from_usize(range.start), |
| 74 | + file.start_pos + BytePos::from_usize(range.end), |
| 75 | + SyntaxContext::root(), |
| 76 | + None, |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +fn check_table(cx: &LateContext<'_>, table: LintTable, groups: &FxHashSet<&str>, file: &SourceFile) { |
| 81 | + let mut by_priority = BTreeMap::<_, LintsAndGroups>::new(); |
| 82 | + for (name, config) in table { |
| 83 | + let lints_and_groups = by_priority.entry(config.as_ref().priority()).or_default(); |
| 84 | + if groups.contains(name.get_ref().as_str()) { |
| 85 | + lints_and_groups.groups.push((name, config)); |
| 86 | + } else { |
| 87 | + lints_and_groups.lints.push(name); |
| 88 | + } |
| 89 | + } |
| 90 | + let low_priority = by_priority |
| 91 | + .iter() |
| 92 | + .find(|(_, lints_and_groups)| !lints_and_groups.lints.is_empty()) |
| 93 | + .map_or(-1, |(&lowest_lint_priority, _)| lowest_lint_priority - 1); |
| 94 | + |
| 95 | + for (priority, LintsAndGroups { lints, groups }) in by_priority { |
| 96 | + let Some(last_lint_alphabetically) = lints.last() else { |
| 97 | + continue; |
| 98 | + }; |
| 99 | + |
| 100 | + for (group, config) in groups { |
| 101 | + span_lint_and_then( |
| 102 | + cx, |
| 103 | + LINT_GROUPS_PRIORITY, |
| 104 | + toml_span(group.span(), file), |
| 105 | + &format!( |
| 106 | + "lint group `{}` has the same priority ({priority}) as a lint", |
| 107 | + group.as_ref() |
| 108 | + ), |
| 109 | + |diag| { |
| 110 | + let config_span = toml_span(config.span(), file); |
| 111 | + if config.as_ref().is_implicit() { |
| 112 | + diag.span_label(config_span, "has an implicit priority of 0"); |
| 113 | + } |
| 114 | + // add the label to next lint after this group that has the same priority |
| 115 | + let lint = lints |
| 116 | + .iter() |
| 117 | + .filter(|lint| lint.span().start > group.span().start) |
| 118 | + .min_by_key(|lint| lint.span().start) |
| 119 | + .unwrap_or(last_lint_alphabetically); |
| 120 | + diag.span_label(toml_span(lint.span(), file), "has the same priority as this lint"); |
| 121 | + diag.note("the order of the lints in the table is ignored by Cargo"); |
| 122 | + let mut suggestion = String::new(); |
| 123 | + Serialize::serialize( |
| 124 | + &LintConfigTable { |
| 125 | + level: config.as_ref().level().into(), |
| 126 | + priority: Some(low_priority), |
| 127 | + }, |
| 128 | + toml::ser::ValueSerializer::new(&mut suggestion), |
| 129 | + ) |
| 130 | + .unwrap(); |
| 131 | + diag.span_suggestion_verbose( |
| 132 | + config_span, |
| 133 | + format!( |
| 134 | + "to have lints override the group set `{}` to a lower priority", |
| 135 | + group.as_ref() |
| 136 | + ), |
| 137 | + suggestion, |
| 138 | + Applicability::MaybeIncorrect, |
| 139 | + ); |
| 140 | + }, |
| 141 | + ); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +pub fn check(cx: &LateContext<'_>) { |
| 147 | + if let Ok(file) = cx.tcx.sess.source_map().load_file(Path::new("Cargo.toml")) |
| 148 | + && let Some(src) = file.src.as_deref() |
| 149 | + && let Ok(cargo_toml) = toml::from_str::<CargoToml>(src) |
| 150 | + { |
| 151 | + let mut rustc_groups = FxHashSet::default(); |
| 152 | + let mut clippy_groups = FxHashSet::default(); |
| 153 | + for (group, ..) in unerased_lint_store(cx.tcx.sess).get_lint_groups() { |
| 154 | + match group.split_once("::") { |
| 155 | + None => { |
| 156 | + rustc_groups.insert(group); |
| 157 | + }, |
| 158 | + Some(("clippy", group)) => { |
| 159 | + clippy_groups.insert(group); |
| 160 | + }, |
| 161 | + _ => {}, |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + check_table(cx, cargo_toml.lints.rust, &rustc_groups, &file); |
| 166 | + check_table(cx, cargo_toml.lints.clippy, &clippy_groups, &file); |
| 167 | + } |
| 168 | +} |
0 commit comments