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