mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:16 +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 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);
|
||||||
|
|
|
@ -68,33 +68,36 @@ 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()) {
|
||||||
let noqa_lineno = noqa_line_for.get(&parent_lineno).unwrap_or(&parent_lineno);
|
if parent_lineno != diagnostic_lineno {
|
||||||
if commented_lines.contains(noqa_lineno) {
|
let noqa_lineno = noqa_line_for.get(&parent_lineno).unwrap_or(&parent_lineno);
|
||||||
let noqa = noqa_directives.entry(noqa_lineno - 1).or_insert_with(|| {
|
if commented_lines.contains(noqa_lineno) {
|
||||||
(noqa::extract_noqa_directive(lines[noqa_lineno - 1]), vec![])
|
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) => {
|
match noqa {
|
||||||
matches.push(diagnostic.kind.rule().noqa_code());
|
(Directive::All(..), matches) => {
|
||||||
ignored_diagnostics.push(index);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
(Directive::Codes(.., codes, _), matches) => {
|
|
||||||
if noqa::includes(diagnostic.kind.rule(), codes) {
|
|
||||||
matches.push(diagnostic.kind.rule().noqa_code());
|
matches.push(diagnostic.kind.rule().noqa_code());
|
||||||
ignored_diagnostics.push(index);
|
ignored_diagnostics.push(index);
|
||||||
continue;
|
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?
|
// 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);
|
||||||
|
|
|
@ -233,26 +233,29 @@ 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()) {
|
||||||
let noqa_lineno = noqa_line_for.get(&parent_lineno).unwrap_or(&parent_lineno);
|
if parent_lineno != diagnostic_lineno {
|
||||||
if commented_lines.contains(noqa_lineno) {
|
let noqa_lineno = noqa_line_for.get(&parent_lineno).unwrap_or(&parent_lineno);
|
||||||
match extract_noqa_directive(lines[noqa_lineno - 1]) {
|
if commented_lines.contains(noqa_lineno) {
|
||||||
Directive::All(..) => {
|
match extract_noqa_directive(lines[noqa_lineno - 1]) {
|
||||||
continue;
|
Directive::All(..) => {
|
||||||
}
|
|
||||||
Directive::Codes(.., codes, _) => {
|
|
||||||
if includes(diagnostic.kind.rule(), &codes) {
|
|
||||||
continue;
|
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?
|
// 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);
|
||||||
|
|
|
@ -22,5 +22,7 @@ expression: diagnostics
|
||||||
end_location:
|
end_location:
|
||||||
row: 4
|
row: 4
|
||||||
column: 34
|
column: 34
|
||||||
parent: ~
|
parent:
|
||||||
|
row: 4
|
||||||
|
column: 0
|
||||||
|
|
||||||
|
|
|
@ -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"
|
||||||
|
|
|
@ -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"
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -22,5 +22,7 @@ expression: diagnostics
|
||||||
end_location:
|
end_location:
|
||||||
row: 4
|
row: 4
|
||||||
column: 24
|
column: 24
|
||||||
parent: ~
|
parent:
|
||||||
|
row: 4
|
||||||
|
column: 0
|
||||||
|
|
||||||
|
|
|
@ -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`)"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue