|
| 1 | +use crate::noqa::{Code, Directive}; |
| 2 | +use crate::registry::Rule; |
| 3 | +use crate::Locator; |
| 4 | +use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; |
| 5 | +use ruff_macros::{derive_message_formats, ViolationMetadata}; |
| 6 | +use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; |
| 7 | + |
| 8 | +use crate::noqa::{Codes, NoqaDirectives}; |
| 9 | + |
| 10 | +/// ## What it does |
| 11 | +/// Checks for `noqa` codes that are invalid. |
| 12 | +/// |
| 13 | +/// ## Why is this bad? |
| 14 | +/// Invalid rule codes serve no purpose and may indicate outdated code suppressions. |
| 15 | +/// |
| 16 | +/// ## Example |
| 17 | +/// ```python |
| 18 | +/// import os # noqa: XYZ999 |
| 19 | +/// ``` |
| 20 | +/// |
| 21 | +/// Use instead: |
| 22 | +/// ```python |
| 23 | +/// import os |
| 24 | +/// ``` |
| 25 | +/// |
| 26 | +/// Or if there are still valid codes needed: |
| 27 | +/// ```python |
| 28 | +/// import os # noqa: E402 |
| 29 | +/// ``` |
| 30 | +/// |
| 31 | +/// ## Options |
| 32 | +/// - `lint.external` |
| 33 | +#[derive(ViolationMetadata)] |
| 34 | +pub(crate) struct InvalidRuleCode { |
| 35 | + pub(crate) rule_code: String, |
| 36 | +} |
| 37 | + |
| 38 | +impl AlwaysFixableViolation for InvalidRuleCode { |
| 39 | + #[derive_message_formats] |
| 40 | + fn message(&self) -> String { |
| 41 | + format!("Invalid rule code in `# noqa`: {}", self.rule_code) |
| 42 | + } |
| 43 | + |
| 44 | + fn fix_title(&self) -> String { |
| 45 | + "Remove the rule code".to_string() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/// RUF102 for invalid noqa codes |
| 50 | +pub(crate) fn invalid_noqa_code( |
| 51 | + diagnostics: &mut Vec<Diagnostic>, |
| 52 | + noqa_directives: &NoqaDirectives, |
| 53 | + locator: &Locator, |
| 54 | + external: &[String], |
| 55 | +) { |
| 56 | + for line in noqa_directives.lines() { |
| 57 | + let Directive::Codes(directive) = &line.directive else { |
| 58 | + continue; |
| 59 | + }; |
| 60 | + |
| 61 | + let all_valid = directive.iter().all(|code| code_is_valid(code, external)); |
| 62 | + |
| 63 | + if all_valid { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + let (valid_codes, invalid_codes): (Vec<_>, Vec<_>) = directive |
| 68 | + .iter() |
| 69 | + .partition(|&code| code_is_valid(code, external)); |
| 70 | + |
| 71 | + if valid_codes.is_empty() { |
| 72 | + diagnostics.push(all_codes_invalid_diagnostic(directive, invalid_codes)); |
| 73 | + } else { |
| 74 | + diagnostics.extend(invalid_codes.into_iter().map(|invalid_code| { |
| 75 | + some_codes_are_invalid_diagnostic(directive, invalid_code, locator) |
| 76 | + })); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +fn code_is_valid(code: &Code, external: &[String]) -> bool { |
| 82 | + let code_str = code.as_str(); |
| 83 | + Rule::from_code(code_str).is_ok() || external.iter().any(|ext| code_str.starts_with(ext)) |
| 84 | +} |
| 85 | + |
| 86 | +fn all_codes_invalid_diagnostic( |
| 87 | + directive: &Codes<'_>, |
| 88 | + invalid_codes: Vec<&Code<'_>>, |
| 89 | +) -> Diagnostic { |
| 90 | + Diagnostic::new( |
| 91 | + InvalidRuleCode { |
| 92 | + rule_code: invalid_codes |
| 93 | + .into_iter() |
| 94 | + .map(Code::as_str) |
| 95 | + .collect::<Vec<_>>() |
| 96 | + .join(", "), |
| 97 | + }, |
| 98 | + directive.range(), |
| 99 | + ) |
| 100 | + .with_fix(Fix::safe_edit(Edit::range_deletion(directive.range()))) |
| 101 | +} |
| 102 | + |
| 103 | +fn some_codes_are_invalid_diagnostic( |
| 104 | + codes: &Codes, |
| 105 | + invalid_code: &Code, |
| 106 | + locator: &Locator, |
| 107 | +) -> Diagnostic { |
| 108 | + let diagnostic = Diagnostic::new( |
| 109 | + InvalidRuleCode { |
| 110 | + rule_code: invalid_code.to_string(), |
| 111 | + }, |
| 112 | + invalid_code.range(), |
| 113 | + ); |
| 114 | + diagnostic.with_fix(Fix::safe_edit(remove_invalid_noqa( |
| 115 | + codes, |
| 116 | + invalid_code, |
| 117 | + locator, |
| 118 | + ))) |
| 119 | +} |
| 120 | + |
| 121 | +fn remove_invalid_noqa(codes: &Codes, invalid_code: &Code, locator: &Locator) -> Edit { |
| 122 | + // Is this the first code after the `:` that needs to get deleted |
| 123 | + // For the first element, delete from after the `:` to the next comma (including) |
| 124 | + // For any other element, delete from the previous comma (including) to the next comma (excluding) |
| 125 | + let mut first = false; |
| 126 | + |
| 127 | + // Find the index of the previous comma or colon. |
| 128 | + let start = locator |
| 129 | + .slice(TextRange::new(codes.start(), invalid_code.start())) |
| 130 | + .rmatch_indices([',', ':']) |
| 131 | + .next() |
| 132 | + .map(|(offset, text)| { |
| 133 | + let offset = codes.start() + TextSize::try_from(offset).unwrap(); |
| 134 | + if text == ":" { |
| 135 | + first = true; |
| 136 | + // Don't include the colon in the deletion range, or the noqa comment becomes invalid |
| 137 | + offset + ':'.text_len() |
| 138 | + } else { |
| 139 | + offset |
| 140 | + } |
| 141 | + }) |
| 142 | + .unwrap_or(invalid_code.start()); |
| 143 | + |
| 144 | + // Find the index of the trailing comma (if any) |
| 145 | + let end = locator |
| 146 | + .slice(TextRange::new(invalid_code.end(), codes.end())) |
| 147 | + .find(',') |
| 148 | + .map(|offset| { |
| 149 | + let offset = invalid_code.end() + TextSize::try_from(offset).unwrap(); |
| 150 | + |
| 151 | + if first { |
| 152 | + offset + ','.text_len() |
| 153 | + } else { |
| 154 | + offset |
| 155 | + } |
| 156 | + }) |
| 157 | + .unwrap_or(invalid_code.end()); |
| 158 | + |
| 159 | + Edit::range_deletion(TextRange::new(start, end)) |
| 160 | +} |
0 commit comments