mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:44:56 +00:00
Set parents even in same-line cases (#3773)
This commit is contained in:
parent
e88fbae926
commit
990b378c4d
9 changed files with 64 additions and 44 deletions
|
@ -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);
|
||||
|
|
|
@ -68,33 +68,36 @@ 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()) {
|
||||
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(|| {
|
||||
(noqa::extract_noqa_directive(lines[noqa_lineno - 1]), vec![])
|
||||
});
|
||||
match noqa {
|
||||
(Directive::All(..), matches) => {
|
||||
matches.push(diagnostic.kind.rule().noqa_code());
|
||||
ignored_diagnostics.push(index);
|
||||
continue;
|
||||
}
|
||||
(Directive::Codes(.., codes, _), matches) => {
|
||||
if noqa::includes(diagnostic.kind.rule(), codes) {
|
||||
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(|| {
|
||||
(noqa::extract_noqa_directive(lines[noqa_lineno - 1]), vec![])
|
||||
});
|
||||
match noqa {
|
||||
(Directive::All(..), matches) => {
|
||||
matches.push(diagnostic.kind.rule().noqa_code());
|
||||
ignored_diagnostics.push(index);
|
||||
continue;
|
||||
}
|
||||
(Directive::Codes(.., codes, _), matches) => {
|
||||
if noqa::includes(diagnostic.kind.rule(), codes) {
|
||||
matches.push(diagnostic.kind.rule().noqa_code());
|
||||
ignored_diagnostics.push(index);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
(Directive::None, ..) => {}
|
||||
}
|
||||
(Directive::None, ..) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
|
|
@ -233,26 +233,29 @@ 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()) {
|
||||
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]) {
|
||||
Directive::All(..) => {
|
||||
continue;
|
||||
}
|
||||
Directive::Codes(.., codes, _) => {
|
||||
if includes(diagnostic.kind.rule(), &codes) {
|
||||
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]) {
|
||||
Directive::All(..) => {
|
||||
continue;
|
||||
}
|
||||
Directive::Codes(.., codes, _) => {
|
||||
if includes(diagnostic.kind.rule(), &codes) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
Directive::None => {}
|
||||
}
|
||||
Directive::None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
|
|
@ -22,5 +22,7 @@ expression: diagnostics
|
|||
end_location:
|
||||
row: 4
|
||||
column: 34
|
||||
parent: ~
|
||||
parent:
|
||||
row: 4
|
||||
column: 0
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -22,5 +22,7 @@ expression: diagnostics
|
|||
end_location:
|
||||
row: 4
|
||||
column: 24
|
||||
parent: ~
|
||||
parent:
|
||||
row: 4
|
||||
column: 0
|
||||
|
||||
|
|
|
@ -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`)"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue