Check newline ending on contents directly (#365)

This commit is contained in:
Charlie Marsh 2022-10-08 17:25:22 -04:00 committed by GitHub
parent 5ccd907398
commit d0e1612507
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 30 deletions

2
resources/test/fixtures/W292_2.py vendored Normal file
View file

@ -0,0 +1,2 @@
def fn() -> None:
pass

View file

@ -114,10 +114,10 @@ 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)
@ -151,7 +151,6 @@ pub fn check_lines(
}
}
}
}
// Enforce that the noqa directive was actually used.
if enforce_noqa {

View file

@ -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(

View file

@ -0,0 +1,6 @@
---
source: src/linter.rs
expression: checks
---
[]