|
| 1 | + |
| 2 | +use rustc_hir::lints::{AttributeLintKind, RenamedLintSuggestion}; |
| 3 | +use rustc_middle::bug; |
| 4 | +use rustc_session::lint::builtin::UNKNOWN_LINTS; |
| 5 | +use rustc_session::{lint::builtin::RENAMED_AND_REMOVED_LINTS}; |
| 6 | +use rustc_session::lint::{CheckLintNameResult}; |
| 7 | +use crate::session_diagnostics::UnknownToolInScopedLint; |
| 8 | + |
| 9 | +use super::prelude::*; |
| 10 | + |
| 11 | +#[derive(Default)] |
| 12 | +pub(crate) struct AllowParser { |
| 13 | + lint_ids: ThinVec<(Symbol, Span)>, |
| 14 | + reason: Option<Symbol>, |
| 15 | + errored: bool, |
| 16 | +} |
| 17 | + |
| 18 | +// Needs to be manually impl:ed as `AttributeParser`, because otherwise deduplication occurs |
| 19 | +impl<S: Stage> AttributeParser<S> for AllowParser { |
| 20 | + const ATTRIBUTES: AcceptMapping<Self, S> = &[( |
| 21 | + &[sym::allow], |
| 22 | + template!( |
| 23 | + List: &["lint1", "lint1, lint2, ...", r#"lint1, lint2, lint3, reason = "...""#], |
| 24 | + "https://doc.rust-lang.org/reference/attributes/diagnostics.html#lint-check-attributes" |
| 25 | + ), |
| 26 | + |this, cx, args| { |
| 27 | + validate_lint_attr(cx, args, &mut this.lint_ids, &mut this.errored, &mut this.reason); |
| 28 | + }, |
| 29 | + )]; |
| 30 | + |
| 31 | + fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> { |
| 32 | + if !self.lint_ids.is_empty() && !self.errored { |
| 33 | + Some(AttributeKind::Allow { lint_ids: self.lint_ids, reason: self.reason }) |
| 34 | + } else { |
| 35 | + None |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); |
| 40 | +} |
| 41 | +#[derive(Default)] |
| 42 | +pub(crate) struct DenyParser { |
| 43 | + lint_ids: ThinVec<(Symbol, Span)>, |
| 44 | + reason: Option<Symbol>, |
| 45 | + errored: bool, |
| 46 | +} |
| 47 | +impl<S: Stage> AttributeParser<S> for DenyParser { |
| 48 | + const ATTRIBUTES: AcceptMapping<Self, S> = &[( |
| 49 | + &[sym::deny], |
| 50 | + template!( |
| 51 | + List: &["lint1", "lint1, lint2, ...", r#"lint1, lint2, lint3, reason = "...""#], |
| 52 | + "https://doc.rust-lang.org/reference/attributes/diagnostics.html#lint-check-attributes" |
| 53 | + ), |
| 54 | + |this, cx, args| { |
| 55 | + validate_lint_attr(cx, args, &mut this.lint_ids, &mut this.errored, &mut this.reason); |
| 56 | + }, |
| 57 | + )]; |
| 58 | + |
| 59 | + fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> { |
| 60 | + if !self.lint_ids.is_empty() && !self.errored { |
| 61 | + Some(AttributeKind::Deny { lint_ids: self.lint_ids, reason: self.reason }) |
| 62 | + } else { |
| 63 | + None |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +fn validate_lint_attr<S: Stage>( |
| 72 | + cx: &mut AcceptContext<'_, '_, S>, |
| 73 | + args: &ArgParser, |
| 74 | + lint_ids: &mut ThinVec<(Symbol, Span)>, |
| 75 | + errored: &mut bool, |
| 76 | + reason: &mut Option<Symbol>, |
| 77 | +) { |
| 78 | + |
| 79 | + let Some(list) = args.list().map(MetaItemListParser::mixed) else { |
| 80 | + cx.expected_list(cx.inner_span, args); |
| 81 | + return; |
| 82 | + }; |
| 83 | + |
| 84 | + let mut list = list.peekable(); |
| 85 | + |
| 86 | + while let Some(item) = list.next() { |
| 87 | + let Some(meta_item) = item.meta_item() else { |
| 88 | + cx.expected_string_literal(item.span(), item.lit()); |
| 89 | + *errored = true; |
| 90 | + return; |
| 91 | + }; |
| 92 | + if let Some(args) = meta_item.word_is(sym::reason) { |
| 93 | + let ArgParser::NameValue(nv) = args else { |
| 94 | + cx.expected_name_value(meta_item.span(), Some(sym::reason)); |
| 95 | + *errored = true; |
| 96 | + continue; |
| 97 | + }; |
| 98 | + if list.peek().is_some() { |
| 99 | + // TODO: proper error |
| 100 | + *errored = true; |
| 101 | + continue; |
| 102 | + } |
| 103 | + let Some(s) = nv.value_as_str() else { |
| 104 | + cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit())); |
| 105 | + *errored = true; |
| 106 | + continue; |
| 107 | + }; |
| 108 | + *reason = Some(s); |
| 109 | + } else { |
| 110 | + let mut segments = meta_item.path().segments(); |
| 111 | + |
| 112 | + let Some(tool_or_name) = segments.next() else { |
| 113 | + bug!("first segment should always exist"); |
| 114 | + }; |
| 115 | + |
| 116 | + let rest = segments.collect::<Vec<_>>(); |
| 117 | + let (tool_name, tool_span, name): (Option<Symbol>, Option<Span>, _) = if rest.is_empty() { |
| 118 | + let name = tool_or_name.name; |
| 119 | + (None,None, name.to_string()) |
| 120 | + } else { |
| 121 | + let tool = tool_or_name; |
| 122 | + let name = |
| 123 | + rest.into_iter().map(|ident| ident.to_string()).collect::<Vec<_>>().join("::"); |
| 124 | + (Some(tool.name), Some(tool.span), name) |
| 125 | + }; |
| 126 | + |
| 127 | + if let Some(ids) = check_lint(cx, &name, tool_name, tool_span, meta_item.span()) { |
| 128 | + lint_ids.extend(ids); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +fn check_lint<S: Stage>( |
| 135 | + cx: &mut AcceptContext<'_, '_, S>, |
| 136 | + name: &str, |
| 137 | + tool_name: Option<Symbol>, |
| 138 | + tool_span: Option<Span>, |
| 139 | + meta_item_span: Span, |
| 140 | +) -> Option<Vec<(Symbol, Span)>> { |
| 141 | + let Some(lint_store) = &cx.sess.lint_store else { |
| 142 | + bug!("lint_store required while parsing attributes"); |
| 143 | + }; |
| 144 | + let mut lint_ids = Vec::new(); |
| 145 | + match lint_store.check_lint_name(name, tool_name, &cx.tools) { |
| 146 | + CheckLintNameResult::Ok(ids) => { |
| 147 | + for id in ids { |
| 148 | + lint_ids.push((Symbol::intern(&id.to_string()), meta_item_span)); |
| 149 | + } |
| 150 | + } |
| 151 | + // TODO fix tools |
| 152 | + CheckLintNameResult::Tool(ids, new_lint_name) => { |
| 153 | + let _name = match new_lint_name { |
| 154 | + None => { |
| 155 | + let complete_name = &format!("{}::{}", tool_name.unwrap(), name); |
| 156 | + Symbol::intern(complete_name) |
| 157 | + } |
| 158 | + Some(new_lint_name) => { |
| 159 | + let new_lint_name = Symbol::intern(&new_lint_name); |
| 160 | + cx.emit_lint(RENAMED_AND_REMOVED_LINTS,AttributeLintKind::DeprecatedLintName { |
| 161 | + name: Symbol::intern(name), |
| 162 | + suggestion: meta_item_span, |
| 163 | + replace: new_lint_name, |
| 164 | + }, |
| 165 | + meta_item_span,); |
| 166 | + new_lint_name |
| 167 | + |
| 168 | + } |
| 169 | + }; |
| 170 | + for id in ids { |
| 171 | + lint_ids.push((Symbol::intern(&id.to_string()), meta_item_span)); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + CheckLintNameResult::MissingTool => { |
| 176 | + // If `MissingTool` is returned, then either the lint does not |
| 177 | + // exist in the tool or the code was not compiled with the tool and |
| 178 | + // therefore the lint was never added to the `LintStore`. To detect |
| 179 | + // this is the responsibility of the lint tool. |
| 180 | + return None; |
| 181 | + } |
| 182 | + |
| 183 | + CheckLintNameResult::NoTool => { |
| 184 | + cx.emit_err(UnknownToolInScopedLint { |
| 185 | + span: tool_span, |
| 186 | + tool_name: tool_name.unwrap(), |
| 187 | + lint_name: name.to_owned(), |
| 188 | + is_nightly_build: cx.sess.is_nightly_build(), |
| 189 | + }); |
| 190 | + return None; |
| 191 | + } |
| 192 | + |
| 193 | + CheckLintNameResult::Renamed(replace) => { |
| 194 | + let name = |
| 195 | + tool_name.map(|tool| format!("{tool}::{name}")).unwrap_or(name.to_string()); |
| 196 | + cx.emit_lint(RENAMED_AND_REMOVED_LINTS, AttributeLintKind::RenamedLint { |
| 197 | + name: Symbol::intern(&name), |
| 198 | + replace, |
| 199 | + suggestion: RenamedLintSuggestion::WithSpan {suggestion: meta_item_span} |
| 200 | + }, meta_item_span); |
| 201 | + |
| 202 | + // If this lint was renamed, apply the new lint instead of ignoring the |
| 203 | + // attribute. Ignore any errors or warnings that happen because the new |
| 204 | + // name is inaccurate. |
| 205 | + // NOTE: `new_name` already includes the tool name, so we don't |
| 206 | + // have to add it again. |
| 207 | + let CheckLintNameResult::Ok(ids) = lint_store.check_lint_name(replace.as_str(), None, &cx.tools) |
| 208 | + else { |
| 209 | + panic!("renamed lint does not exist: {replace}"); |
| 210 | + }; |
| 211 | + |
| 212 | + for id in ids { |
| 213 | + let name = Symbol::intern(&id.to_string()); |
| 214 | + lint_ids.push((name, meta_item_span)); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + CheckLintNameResult::Removed(reason) => { |
| 219 | + let name = |
| 220 | + tool_name.map(|tool| format!("{tool}::{name}")).unwrap_or(name.to_owned()); |
| 221 | + cx.emit_lint(RENAMED_AND_REMOVED_LINTS, AttributeLintKind::RemovedLint {name, reason}, meta_item_span); |
| 222 | + return None; |
| 223 | + } |
| 224 | + |
| 225 | + CheckLintNameResult::NoLint(suggestion) => { |
| 226 | + let name = |
| 227 | + tool_name.map(|tool| format!("{tool}::{name}")).unwrap_or(name.to_owned()); |
| 228 | + cx.emit_lint(UNKNOWN_LINTS, AttributeLintKind::UnknownLint { name, suggestion, span: meta_item_span }, meta_item_span); |
| 229 | + return None; |
| 230 | + } |
| 231 | + } |
| 232 | + Some(lint_ids) |
| 233 | +} |
0 commit comments