Skip to content

Commit 73851e7

Browse files
dhruvmanilaMichaReiser
authored andcommitted
Avoid displaying syntax error as log message (#11902)
## Summary Follow-up to #11901 This PR avoids displaying the syntax errors as log message now that the `E999` diagnostic cannot be disabled. For context on why this was added, refer to #2505. Basically, we would allow ignoring the syntax error diagnostic because certain syntax feature weren't supported back then like `match` statement. And, if a user ignored `E999`, Ruff would give no feedback if the source code contained any syntax error. So, this log message was a way to indicate to the user even if `E999` was disabled. The current state of the parser is such that (a) it matches with the latest grammar and (b) it's easy to add support for any new syntax. **Note:** This PR doesn't remove the `DisplayParseError` struct because it's still being used by the formatter. ## Test Plan Update existing snapshots from the integration tests.
1 parent e7b4969 commit 73851e7

File tree

3 files changed

+60
-101
lines changed

3 files changed

+60
-101
lines changed

crates/ruff/src/diagnostics.rs

Lines changed: 59 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ use std::path::Path;
99

1010
use anyhow::{Context, Result};
1111
use colored::Colorize;
12-
use log::{debug, error, warn};
13-
use ruff_linter::codes::Rule;
12+
use log::{debug, warn};
1413
use rustc_hash::FxHashMap;
1514

1615
use ruff_diagnostics::Diagnostic;
16+
use ruff_linter::codes::Rule;
1717
use ruff_linter::linter::{lint_fix, lint_only, FixTable, FixerResult, LinterResult, ParseSource};
18-
use ruff_linter::logging::DisplayParseError;
1918
use ruff_linter::message::{Message, SyntaxErrorMessage};
2019
use ruff_linter::pyproject_toml::lint_pyproject_toml;
2120
use ruff_linter::settings::types::UnsafeFixes;
@@ -357,13 +356,6 @@ pub(crate) fn lint_path(
357356
}
358357
}
359358

360-
if let Some(error) = parse_error {
361-
error!(
362-
"{}",
363-
DisplayParseError::from_source_kind(error, Some(path.to_path_buf()), &transformed)
364-
);
365-
}
366-
367359
let notebook_indexes = if let SourceKind::IpyNotebook(notebook) = transformed {
368360
FxHashMap::from_iter([(path.to_string_lossy().to_string(), notebook.into_index())])
369361
} else {
@@ -408,52 +400,66 @@ pub(crate) fn lint_stdin(
408400
};
409401

410402
// Lint the inputs.
411-
let (
412-
LinterResult {
413-
data: messages,
414-
error: parse_error,
415-
},
416-
transformed,
417-
fixed,
418-
) = if matches!(fix_mode, flags::FixMode::Apply | flags::FixMode::Diff) {
419-
if let Ok(FixerResult {
420-
result,
421-
transformed,
422-
fixed,
423-
}) = lint_fix(
424-
path.unwrap_or_else(|| Path::new("-")),
425-
package,
426-
noqa,
427-
settings.unsafe_fixes,
428-
&settings.linter,
429-
&source_kind,
430-
source_type,
431-
) {
432-
match fix_mode {
433-
flags::FixMode::Apply => {
434-
// Write the contents to stdout, regardless of whether any errors were fixed.
435-
transformed.write(&mut io::stdout().lock())?;
436-
}
437-
flags::FixMode::Diff => {
438-
// But only write a diff if it's non-empty.
439-
if !fixed.is_empty() {
440-
write!(
441-
&mut io::stdout().lock(),
442-
"{}",
443-
source_kind.diff(&transformed, path).unwrap()
444-
)?;
403+
let (LinterResult { data: messages, .. }, transformed, fixed) =
404+
if matches!(fix_mode, flags::FixMode::Apply | flags::FixMode::Diff) {
405+
if let Ok(FixerResult {
406+
result,
407+
transformed,
408+
fixed,
409+
}) = lint_fix(
410+
path.unwrap_or_else(|| Path::new("-")),
411+
package,
412+
noqa,
413+
settings.unsafe_fixes,
414+
&settings.linter,
415+
&source_kind,
416+
source_type,
417+
) {
418+
match fix_mode {
419+
flags::FixMode::Apply => {
420+
// Write the contents to stdout, regardless of whether any errors were fixed.
421+
transformed.write(&mut io::stdout().lock())?;
422+
}
423+
flags::FixMode::Diff => {
424+
// But only write a diff if it's non-empty.
425+
if !fixed.is_empty() {
426+
write!(
427+
&mut io::stdout().lock(),
428+
"{}",
429+
source_kind.diff(&transformed, path).unwrap()
430+
)?;
431+
}
445432
}
433+
flags::FixMode::Generate => {}
446434
}
447-
flags::FixMode::Generate => {}
448-
}
449-
let transformed = if let Cow::Owned(transformed) = transformed {
450-
transformed
435+
let transformed = if let Cow::Owned(transformed) = transformed {
436+
transformed
437+
} else {
438+
source_kind
439+
};
440+
(result, transformed, fixed)
451441
} else {
452-
source_kind
453-
};
454-
(result, transformed, fixed)
442+
// If we fail to fix, lint the original source code.
443+
let result = lint_only(
444+
path.unwrap_or_else(|| Path::new("-")),
445+
package,
446+
&settings.linter,
447+
noqa,
448+
&source_kind,
449+
source_type,
450+
ParseSource::None,
451+
);
452+
453+
// Write the contents to stdout anyway.
454+
if fix_mode.is_apply() {
455+
source_kind.write(&mut io::stdout().lock())?;
456+
}
457+
458+
let transformed = source_kind;
459+
let fixed = FxHashMap::default();
460+
(result, transformed, fixed)
461+
}
455462
} else {
456-
// If we fail to fix, lint the original source code.
457463
let result = lint_only(
458464
path.unwrap_or_else(|| Path::new("-")),
459465
package,
@@ -463,37 +469,10 @@ pub(crate) fn lint_stdin(
463469
source_type,
464470
ParseSource::None,
465471
);
466-
467-
// Write the contents to stdout anyway.
468-
if fix_mode.is_apply() {
469-
source_kind.write(&mut io::stdout().lock())?;
470-
}
471-
472472
let transformed = source_kind;
473473
let fixed = FxHashMap::default();
474474
(result, transformed, fixed)
475-
}
476-
} else {
477-
let result = lint_only(
478-
path.unwrap_or_else(|| Path::new("-")),
479-
package,
480-
&settings.linter,
481-
noqa,
482-
&source_kind,
483-
source_type,
484-
ParseSource::None,
485-
);
486-
let transformed = source_kind;
487-
let fixed = FxHashMap::default();
488-
(result, transformed, fixed)
489-
};
490-
491-
if let Some(error) = parse_error {
492-
error!(
493-
"{}",
494-
DisplayParseError::from_source_kind(error, path.map(Path::to_path_buf), &transformed)
495-
);
496-
}
475+
};
497476

498477
let notebook_indexes = if let SourceKind::IpyNotebook(notebook) = transformed {
499478
FxHashMap::from_iter([(

crates/ruff/tests/integration_test.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,6 @@ fn stdin_parse_error() {
807807
Found 1 error.
808808
809809
----- stderr -----
810-
error: Failed to parse at 1:16: Expected one or more symbol names after import
811810
"###);
812811
}
813812

@@ -836,7 +835,6 @@ fn stdin_multiple_parse_error() {
836835
Found 2 errors.
837836
838837
----- stderr -----
839-
error: Failed to parse at 1:16: Expected one or more symbol names after import
840838
"###);
841839
}
842840

@@ -858,7 +856,6 @@ fn parse_error_not_included() {
858856
Found 1 error.
859857
860858
----- stderr -----
861-
error: Failed to parse at 1:6: Expected an expression
862859
"###);
863860
}
864861

@@ -880,7 +877,6 @@ fn deprecated_parse_error_selection() {
880877
881878
----- stderr -----
882879
warning: Rule `E999` is deprecated and will be removed in a future release. Syntax errors will always be shown regardless of whether this rule is selected or not.
883-
error: Failed to parse at 1:6: Expected an expression
884880
"###);
885881
}
886882

crates/ruff_linter/src/linter.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::path::Path;
55
use anyhow::{anyhow, Result};
66
use colored::Colorize;
77
use itertools::Itertools;
8-
use log::error;
98
use rustc_hash::FxHashMap;
109

1110
use ruff_diagnostics::Diagnostic;
@@ -26,7 +25,6 @@ use crate::checkers::tokens::check_tokens;
2625
use crate::directives::Directives;
2726
use crate::doc_lines::{doc_lines_from_ast, doc_lines_from_tokens};
2827
use crate::fix::{fix_file, FixResult};
29-
use crate::logging::DisplayParseError;
3028
use crate::message::Message;
3129
use crate::noqa::add_noqa;
3230
use crate::registry::{AsRule, Rule, RuleSet};
@@ -354,8 +352,7 @@ pub fn add_noqa_to_path(
354352

355353
// Generate diagnostics, ignoring any existing `noqa` directives.
356354
let LinterResult {
357-
data: diagnostics,
358-
error,
355+
data: diagnostics, ..
359356
} = check_path(
360357
path,
361358
package,
@@ -370,19 +367,6 @@ pub fn add_noqa_to_path(
370367
&parsed,
371368
);
372369

373-
// Log any parse errors.
374-
if let Some(error) = error {
375-
error!(
376-
"{}",
377-
DisplayParseError::from_source_code(
378-
error,
379-
Some(path.to_path_buf()),
380-
&locator.to_source_code(),
381-
source_kind,
382-
)
383-
);
384-
}
385-
386370
// Add any missing `# noqa` pragmas.
387371
// TODO(dhruvmanila): Add support for Jupyter Notebooks
388372
add_noqa(

0 commit comments

Comments
 (0)