Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filename to noqa warnings #5856

Merged
merged 5 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions crates/ruff/src/checkers/noqa.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! `NoQA` enforcement and validation.

use std::path::Path;

use itertools::Itertools;
use ruff_text_size::{TextLen, TextRange, TextSize};
use rustpython_parser::ast::Ranged;
Expand All @@ -16,17 +18,18 @@ use crate::settings::Settings;

pub(crate) fn check_noqa(
diagnostics: &mut Vec<Diagnostic>,
path: &Path,
locator: &Locator,
comment_ranges: &[TextRange],
noqa_line_for: &NoqaMapping,
analyze_directives: bool,
settings: &Settings,
) -> Vec<usize> {
// Identify any codes that are globally exempted (within the current file).
let exemption = FileExemption::try_extract(locator.contents(), comment_ranges, locator);
let exemption = FileExemption::try_extract(locator.contents(), comment_ranges, path, locator);

// Extract all `noqa` directives.
let mut noqa_directives = NoqaDirectives::from_commented_ranges(comment_ranges, locator);
let mut noqa_directives = NoqaDirectives::from_commented_ranges(comment_ranges, path, locator);

// Indices of diagnostics that were ignored by a `noqa` directive.
let mut ignored_diagnostics = vec![];
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ pub fn check_path(
{
let ignored = check_noqa(
&mut diagnostics,
path,
locator,
indexer.comment_ranges(),
&directives.noqa_line_for,
Expand Down
23 changes: 19 additions & 4 deletions crates/ruff/src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use ruff_python_ast::source_code::Locator;
use ruff_python_whitespace::LineEnding;

use crate::codes::NoqaCode;
use crate::fs::relativize_path;
use crate::registry::{AsRule, Rule, RuleSet};
use crate::rule_redirects::get_redirect_target;

Expand Down Expand Up @@ -225,6 +226,7 @@ impl FileExemption {
pub(crate) fn try_extract(
contents: &str,
comment_ranges: &[TextRange],
path: &Path,
locator: &Locator,
) -> Option<Self> {
let mut exempt_codes: Vec<NoqaCode> = vec![];
Expand All @@ -234,7 +236,8 @@ impl FileExemption {
Err(err) => {
#[allow(deprecated)]
let line = locator.compute_line_index(range.start());
warn!("Invalid `# noqa` directive on line {line}: {err}");
let path_display = path.display();
warn!("Invalid `# noqa` directive on {path_display}:{line}: {err}");
}
Ok(Some(ParsedFileExemption::All)) => {
return Some(Self::All);
Expand Down Expand Up @@ -437,6 +440,7 @@ pub(crate) fn add_noqa(
line_ending: LineEnding,
) -> Result<usize> {
let (count, output) = add_noqa_inner(
path,
diagnostics,
locator,
commented_lines,
Expand All @@ -448,6 +452,7 @@ pub(crate) fn add_noqa(
}

fn add_noqa_inner(
path: &Path,
diagnostics: &[Diagnostic],
locator: &Locator,
commented_ranges: &[TextRange],
Expand All @@ -460,8 +465,8 @@ fn add_noqa_inner(

// Whether the file is exempted from all checks.
// Codes that are globally exempted (within the current file).
let exemption = FileExemption::try_extract(locator.contents(), commented_ranges, locator);
let directives = NoqaDirectives::from_commented_ranges(commented_ranges, locator);
let exemption = FileExemption::try_extract(locator.contents(), commented_ranges, path, locator);
let directives = NoqaDirectives::from_commented_ranges(commented_ranges, path, locator);

// Mark any non-ignored diagnostics.
for diagnostic in diagnostics {
Expand Down Expand Up @@ -625,6 +630,7 @@ pub(crate) struct NoqaDirectives<'a> {
impl<'a> NoqaDirectives<'a> {
pub(crate) fn from_commented_ranges(
comment_ranges: &[TextRange],
path: &Path,
locator: &'a Locator<'a>,
) -> Self {
let mut directives = Vec::new();
Expand All @@ -634,7 +640,8 @@ impl<'a> NoqaDirectives<'a> {
Err(err) => {
#[allow(deprecated)]
let line = locator.compute_line_index(range.start());
warn!("Invalid `# noqa` directive on line {line}: {err}");
let path_display = relativize_path(path);
warn!("Invalid `# noqa` directive on {path_display}:{line}: {err}");
}
Ok(Some(directive)) => {
// noqa comments are guaranteed to be single line.
Expand Down Expand Up @@ -758,6 +765,8 @@ impl FromIterator<TextRange> for NoqaMapping {

#[cfg(test)]
mod tests {
use std::path::Path;

use insta::assert_debug_snapshot;
use ruff_text_size::{TextRange, TextSize};

Expand Down Expand Up @@ -946,9 +955,12 @@ mod tests {

#[test]
fn modification() {
let path = Path::new("/tmp/foo.txt");

let contents = "x = 1";
let noqa_line_for = NoqaMapping::default();
let (count, output) = add_noqa_inner(
path,
&[],
&Locator::new(contents),
&[],
Expand All @@ -968,6 +980,7 @@ mod tests {
let contents = "x = 1";
let noqa_line_for = NoqaMapping::default();
let (count, output) = add_noqa_inner(
path,
&diagnostics,
&Locator::new(contents),
&[],
Expand All @@ -992,6 +1005,7 @@ mod tests {
let contents = "x = 1 # noqa: E741\n";
let noqa_line_for = NoqaMapping::default();
let (count, output) = add_noqa_inner(
path,
&diagnostics,
&Locator::new(contents),
&[TextRange::new(TextSize::from(7), TextSize::from(19))],
Expand All @@ -1016,6 +1030,7 @@ mod tests {
let contents = "x = 1 # noqa";
let noqa_line_for = NoqaMapping::default();
let (count, output) = add_noqa_inner(
path,
&diagnostics,
&Locator::new(contents),
&[TextRange::new(TextSize::from(7), TextSize::from(13))],
Expand Down
Loading