diff --git a/crates/ruff/src/noqa.rs b/crates/ruff/src/noqa.rs index e609ceb4a8dfa..ca0ffa959fa85 100644 --- a/crates/ruff/src/noqa.rs +++ b/crates/ruff/src/noqa.rs @@ -248,22 +248,41 @@ impl FileExemption { let path_display = relativize_path(path); warn!("Invalid `# ruff: noqa` directive at {path_display}:{line}: {err}"); } - Ok(Some(ParsedFileExemption::All)) => { - return Some(Self::All); - } - Ok(Some(ParsedFileExemption::Codes(codes))) => { - exempt_codes.extend(codes.into_iter().filter_map(|code| { - if let Ok(rule) = Rule::from_code(get_redirect_target(code).unwrap_or(code)) - { - Some(rule.noqa_code()) - } else { - #[allow(deprecated)] - let line = locator.compute_line_index(range.start()); - let path_display = relativize_path(path); - warn!("Invalid code provided to `# ruff: noqa` at {path_display}:{line}: {code}"); - None + Ok(Some(exemption)) => { + if !locator + .slice(TextRange::new( + locator.line_start(range.start()), + range.start(), + )) + .chars() + .all(char::is_whitespace) + { + #[allow(deprecated)] + let line = locator.compute_line_index(range.start()); + let path_display = relativize_path(path); + warn!("Unexpected end-of-line `# ruff: noqa` directive at {path_display}:{line}"); + continue; + } + + match exemption { + ParsedFileExemption::All => { + return Some(Self::All); } - })); + ParsedFileExemption::Codes(codes) => { + exempt_codes.extend(codes.into_iter().filter_map(|code| { + if let Ok(rule) = Rule::from_code(get_redirect_target(code).unwrap_or(code)) + { + Some(rule.noqa_code()) + } else { + #[allow(deprecated)] + let line = locator.compute_line_index(range.start()); + let path_display = relativize_path(path); + warn!("Invalid code provided to `# ruff: noqa` at {path_display}:{line}: {code}"); + None + } + })); + } + } } Ok(None) => {} }