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,41 +114,40 @@ pub fn check_lines(
} }
// Enforce newlines at end of files. // Enforce newlines at end of files.
if settings.enabled.contains(&CheckCode::W292) { if settings.enabled.contains(&CheckCode::W292) && !contents.ends_with('\n') {
// If the file terminates with a newline, the last line should be an empty string slice. // 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 let Some(line) = lines.last() {
if !line.is_empty() { let lineno = lines.len() - 1;
let lineno = lines.len() - 1; let noqa_lineno = noqa_line_for
let noqa_lineno = noqa_line_for .get(lineno)
.get(lineno) .map(|lineno| lineno - 1)
.map(|lineno| lineno - 1) .unwrap_or(lineno);
.unwrap_or(lineno);
let noqa = noqa_directives let noqa = noqa_directives
.entry(noqa_lineno) .entry(noqa_lineno)
.or_insert_with(|| (noqa::extract_noqa_directive(lines[noqa_lineno]), vec![])); .or_insert_with(|| (noqa::extract_noqa_directive(lines[noqa_lineno]), vec![]));
let check = Check::new( let check = Check::new(
CheckKind::NoNewLineAtEndOfFile, CheckKind::NoNewLineAtEndOfFile,
Range { Range {
location: Location::new(lines.len(), line.len() + 1), location: Location::new(lines.len(), line.len() + 1),
end_location: Location::new(lines.len(), line.len() + 1), end_location: Location::new(lines.len(), line.len() + 1),
}, },
); );
match noqa { match noqa {
(Directive::All(_, _), matches) => { (Directive::All(_, _), matches) => {
matches.push(check.kind.code().as_str()); 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),
} }
(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),
} }
} }
} }

View file

@ -363,6 +363,18 @@ mod tests {
Ok(()) 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] #[test]
fn f401() -> Result<()> { fn f401() -> Result<()> {
let mut checks = check_path( let mut checks = check_path(

View file

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