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

View file

@ -68,8 +68,11 @@ pub fn check_noqa(
FileExemption::None => {}
}
let diagnostic_lineno = diagnostic.location.row();
// Is the violation ignored by a `noqa` directive on the parent line?
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);
if commented_lines.contains(noqa_lineno) {
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?
let diagnostic_lineno = diagnostic.location.row();
let noqa_lineno = noqa_line_for
.get(&diagnostic_lineno)
.unwrap_or(&diagnostic_lineno);

View file

@ -233,8 +233,11 @@ fn add_noqa_inner(
FileExemption::None => {}
}
let diagnostic_lineno = diagnostic.location.row();
// Is the violation ignored by a `noqa` directive on the parent line?
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);
if commented_lines.contains(noqa_lineno) {
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?
let diagnostic_lineno = diagnostic.location.row();
let noqa_lineno = noqa_line_for
.get(&diagnostic_lineno)
.unwrap_or(&diagnostic_lineno);

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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