diff --git a/resources/test/fixtures/W292_2.py b/resources/test/fixtures/W292_2.py new file mode 100644 index 0000000000..d87dddbf7f --- /dev/null +++ b/resources/test/fixtures/W292_2.py @@ -0,0 +1,2 @@ +def fn() -> None: + pass diff --git a/src/check_lines.rs b/src/check_lines.rs index f3b30bebe8..a79b267d62 100644 --- a/src/check_lines.rs +++ b/src/check_lines.rs @@ -114,41 +114,40 @@ pub fn check_lines( } // Enforce newlines at end of files. - if settings.enabled.contains(&CheckCode::W292) { - // If the file terminates with a newline, the last line should be an empty string slice. + if settings.enabled.contains(&CheckCode::W292) && !contents.ends_with('\n') { + // Note: if `lines.last()` is `None`, then `contents` is empty (and so we don't want to + // raise W292 anyway). if let Some(line) = lines.last() { - if !line.is_empty() { - let lineno = lines.len() - 1; - let noqa_lineno = noqa_line_for - .get(lineno) - .map(|lineno| lineno - 1) - .unwrap_or(lineno); + let lineno = lines.len() - 1; + let noqa_lineno = noqa_line_for + .get(lineno) + .map(|lineno| lineno - 1) + .unwrap_or(lineno); - let noqa = noqa_directives - .entry(noqa_lineno) - .or_insert_with(|| (noqa::extract_noqa_directive(lines[noqa_lineno]), vec![])); + let noqa = noqa_directives + .entry(noqa_lineno) + .or_insert_with(|| (noqa::extract_noqa_directive(lines[noqa_lineno]), vec![])); - let check = Check::new( - CheckKind::NoNewLineAtEndOfFile, - Range { - location: Location::new(lines.len(), line.len() + 1), - end_location: Location::new(lines.len(), line.len() + 1), - }, - ); + let check = Check::new( + CheckKind::NoNewLineAtEndOfFile, + Range { + location: Location::new(lines.len(), line.len() + 1), + end_location: Location::new(lines.len(), line.len() + 1), + }, + ); - match noqa { - (Directive::All(_, _), matches) => { - matches.push(check.kind.code().as_str()); - } - (Directive::Codes(_, _, codes), matches) => { - if codes.contains(&check.kind.code().as_str()) { - matches.push(check.kind.code().as_str()); - } else { - line_checks.push(check); - } - } - (Directive::None, _) => line_checks.push(check), + match noqa { + (Directive::All(_, _), matches) => { + matches.push(check.kind.code().as_str()); } + (Directive::Codes(_, _, codes), matches) => { + if codes.contains(&check.kind.code().as_str()) { + matches.push(check.kind.code().as_str()); + } else { + line_checks.push(check); + } + } + (Directive::None, _) => line_checks.push(check), } } } diff --git a/src/linter.rs b/src/linter.rs index 4ba35803d2..e0a9448e89 100644 --- a/src/linter.rs +++ b/src/linter.rs @@ -363,6 +363,18 @@ mod tests { Ok(()) } + #[test] + fn w292_2() -> Result<()> { + let mut checks = check_path( + Path::new("./resources/test/fixtures/W292_2.py"), + &settings::Settings::for_rule(CheckCode::W292), + &fixer::Mode::Generate, + )?; + checks.sort_by_key(|check| check.location); + insta::assert_yaml_snapshot!(checks); + Ok(()) + } + #[test] fn f401() -> Result<()> { let mut checks = check_path( diff --git a/src/snapshots/ruff__linter__tests__w292_2.snap b/src/snapshots/ruff__linter__tests__w292_2.snap new file mode 100644 index 0000000000..60c615f917 --- /dev/null +++ b/src/snapshots/ruff__linter__tests__w292_2.snap @@ -0,0 +1,6 @@ +--- +source: src/linter.rs +expression: checks +--- +[] +