Set parents even in same-line cases (#3773)

This commit is contained in:
Charlie Marsh 2023-03-28 12:09:30 -04:00 committed by GitHub
parent e88fbae926
commit 990b378c4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 64 additions and 44 deletions

View file

@ -4960,9 +4960,7 @@ impl<'a> Checker<'a> {
let child: &Stmt = defined_by.into(); let child: &Stmt = defined_by.into();
let diagnostic_lineno = binding.range.location.row(); let diagnostic_lineno = binding.range.location.row();
let parent_lineno = if matches!(child.node, StmtKind::ImportFrom { .. }) let parent_lineno = if matches!(child.node, StmtKind::ImportFrom { .. }) {
&& child.location.row() != diagnostic_lineno
{
Some(child.location.row()) Some(child.location.row())
} else { } else {
None None
@ -5038,9 +5036,7 @@ impl<'a> Checker<'a> {
}, },
*range, *range,
); );
if matches!(child.node, StmtKind::ImportFrom { .. }) if matches!(child.node, StmtKind::ImportFrom { .. }) {
&& child.location.row() != range.location.row()
{
diagnostic.set_parent(child.location); diagnostic.set_parent(child.location);
} }
if let Some(fix) = fix.as_ref() { if let Some(fix) = fix.as_ref() {
@ -5072,9 +5068,7 @@ impl<'a> Checker<'a> {
}, },
*range, *range,
); );
if matches!(child.node, StmtKind::ImportFrom { .. }) if matches!(child.node, StmtKind::ImportFrom { .. }) {
&& child.location.row() != range.location.row()
{
diagnostic.set_parent(child.location); diagnostic.set_parent(child.location);
} }
diagnostics.push(diagnostic); diagnostics.push(diagnostic);

View file

@ -68,8 +68,11 @@ pub fn check_noqa(
FileExemption::None => {} FileExemption::None => {}
} }
let diagnostic_lineno = diagnostic.location.row();
// Is the violation ignored by a `noqa` directive on the parent line? // Is the violation ignored by a `noqa` directive on the parent line?
if let Some(parent_lineno) = diagnostic.parent.map(|location| location.row()) { if let Some(parent_lineno) = diagnostic.parent.map(|location| location.row()) {
if parent_lineno != diagnostic_lineno {
let noqa_lineno = noqa_line_for.get(&parent_lineno).unwrap_or(&parent_lineno); let noqa_lineno = noqa_line_for.get(&parent_lineno).unwrap_or(&parent_lineno);
if commented_lines.contains(noqa_lineno) { if commented_lines.contains(noqa_lineno) {
let noqa = noqa_directives.entry(noqa_lineno - 1).or_insert_with(|| { let noqa = noqa_directives.entry(noqa_lineno - 1).or_insert_with(|| {
@ -92,9 +95,9 @@ pub fn check_noqa(
} }
} }
} }
}
// Is the diagnostic ignored by a `noqa` directive on the same line? // Is the diagnostic ignored by a `noqa` directive on the same line?
let diagnostic_lineno = diagnostic.location.row();
let noqa_lineno = noqa_line_for let noqa_lineno = noqa_line_for
.get(&diagnostic_lineno) .get(&diagnostic_lineno)
.unwrap_or(&diagnostic_lineno); .unwrap_or(&diagnostic_lineno);

View file

@ -233,8 +233,11 @@ fn add_noqa_inner(
FileExemption::None => {} FileExemption::None => {}
} }
let diagnostic_lineno = diagnostic.location.row();
// Is the violation ignored by a `noqa` directive on the parent line? // Is the violation ignored by a `noqa` directive on the parent line?
if let Some(parent_lineno) = diagnostic.parent.map(|location| location.row()) { if let Some(parent_lineno) = diagnostic.parent.map(|location| location.row()) {
if parent_lineno != diagnostic_lineno {
let noqa_lineno = noqa_line_for.get(&parent_lineno).unwrap_or(&parent_lineno); let noqa_lineno = noqa_line_for.get(&parent_lineno).unwrap_or(&parent_lineno);
if commented_lines.contains(noqa_lineno) { if commented_lines.contains(noqa_lineno) {
match extract_noqa_directive(lines[noqa_lineno - 1]) { match extract_noqa_directive(lines[noqa_lineno - 1]) {
@ -250,9 +253,9 @@ fn add_noqa_inner(
} }
} }
} }
}
// Is the diagnostic ignored by a `noqa` directive on the same line? // Is the diagnostic ignored by a `noqa` directive on the same line?
let diagnostic_lineno = diagnostic.location.row();
let noqa_lineno = noqa_line_for let noqa_lineno = noqa_line_for
.get(&diagnostic_lineno) .get(&diagnostic_lineno)
.unwrap_or(&diagnostic_lineno); .unwrap_or(&diagnostic_lineno);

View file

@ -22,5 +22,7 @@ expression: diagnostics
end_location: end_location:
row: 4 row: 4
column: 34 column: 34
parent: ~ parent:
row: 4
column: 0

View file

@ -22,7 +22,9 @@ expression: diagnostics
end_location: end_location:
row: 3 row: 3
column: 0 column: 0
parent: ~ parent:
row: 2
column: 0
- kind: - kind:
name: UnusedImport name: UnusedImport
body: "`d.e.f` imported but unused" body: "`d.e.f` imported but unused"
@ -43,7 +45,9 @@ expression: diagnostics
end_location: end_location:
row: 4 row: 4
column: 0 column: 0
parent: ~ parent:
row: 3
column: 0
- kind: - kind:
name: UnusedImport name: UnusedImport
body: "`h.i` imported but unused" body: "`h.i` imported but unused"

View file

@ -22,7 +22,9 @@ expression: diagnostics
end_location: end_location:
row: 8 row: 8
column: 0 column: 0
parent: ~ parent:
row: 7
column: 0
- kind: - kind:
name: UnusedImport name: UnusedImport
body: "`.datastructures.UploadFile` imported but unused" body: "`.datastructures.UploadFile` imported but unused"
@ -43,7 +45,9 @@ expression: diagnostics
end_location: end_location:
row: 11 row: 11
column: 0 column: 0
parent: ~ parent:
row: 10
column: 0
- kind: - kind:
name: UnusedImport name: UnusedImport
body: "`background` imported but unused" body: "`background` imported but unused"

View file

@ -45,7 +45,9 @@ expression: diagnostics
end_location: end_location:
row: 67 row: 67
column: 0 column: 0
parent: ~ parent:
row: 66
column: 0
- kind: - kind:
name: UnusedImport name: UnusedImport
body: "`typing.AwaitableGenerator` imported but unused" body: "`typing.AwaitableGenerator` imported but unused"
@ -66,5 +68,7 @@ expression: diagnostics
end_location: end_location:
row: 67 row: 67
column: 0 column: 0
parent: ~ parent:
row: 66
column: 0

View file

@ -22,5 +22,7 @@ expression: diagnostics
end_location: end_location:
row: 4 row: 4
column: 24 column: 24
parent: ~ parent:
row: 4
column: 0

View file

@ -129,7 +129,9 @@ expression: diagnostics
end_location: end_location:
row: 89 row: 89
column: 52 column: 52
parent: ~ parent:
row: 89
column: 4
- kind: - kind:
name: UnusedImport name: UnusedImport
body: "`typing.AwaitableGenerator` imported but unused" body: "`typing.AwaitableGenerator` imported but unused"
@ -150,7 +152,9 @@ expression: diagnostics
end_location: end_location:
row: 89 row: 89
column: 52 column: 52
parent: ~ parent:
row: 89
column: 4
- kind: - kind:
name: UnusedNOQA name: UnusedNOQA
body: "Unused `noqa` directive (non-enabled: `F501`)" body: "Unused `noqa` directive (non-enabled: `F501`)"