Skip to content

Commit

Permalink
Avoid adding noqa comment for syntax error
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Jun 18, 2024
1 parent ad8e4fa commit dee6720
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion crates/ruff_linter/src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,12 @@ fn find_noqa_comments<'a>(

// Mark any non-ignored diagnostics.
for diagnostic in diagnostics {
// Syntax errors cannot be ignored by a `noqa` comment so we shouldn't add them either.
if diagnostic.kind.rule() == Rule::SyntaxError {
comments_by_line.push(None);
continue;
}

match &exemption {
FileExemption::All(_) => {
// If the file is exempted, don't add any noqa directives.
Expand Down Expand Up @@ -1063,7 +1069,7 @@ mod tests {

use crate::generate_noqa_edits;
use crate::noqa::{add_noqa_inner, Directive, NoqaMapping, ParsedFileExemption};
use crate::rules::pycodestyle::rules::AmbiguousVariableName;
use crate::rules::pycodestyle::rules::{AmbiguousVariableName, UselessSemicolon};
use crate::rules::pyflakes::rules::UnusedVariable;
use crate::rules::pyupgrade::rules::PrintfStringFormatting;

Expand Down Expand Up @@ -1380,4 +1386,36 @@ print(
))]
);
}

#[test]
fn syntax_error() {
let path = Path::new("/tmp/foo.txt");
let source = "\
foo;
bar =
";
let diagnostics = [Diagnostic::new(
UselessSemicolon,
TextRange::new(4.into(), 5.into()),
)];
let noqa_line_for = NoqaMapping::default();
let comment_ranges = CommentRanges::default();
let edits = generate_noqa_edits(
path,
&diagnostics,
&Locator::new(source),
&comment_ranges,
&[],
&noqa_line_for,
LineEnding::Lf,
);
assert_eq!(
edits,
vec![Some(Edit::replacement(
" # noqa: E703\n".to_string(),
4.into(),
5.into()
))]
);
}
}

0 comments on commit dee6720

Please sign in to comment.