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

Document LinterResult::has_syntax_error and add Parsed::has_no_errors #16443

Merged
merged 8 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions crates/ruff_linter/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct LinterResult {
pub messages: Vec<Message>,
/// A flag indicating the presence of syntax errors in the source file.
/// If `true`, at least one syntax error was detected in the source file.
///
/// This includes both [`ParseError`]s and [`UnsupportedSyntaxError`]s.
pub has_syntax_error: bool,
}

Expand Down Expand Up @@ -445,7 +447,7 @@ pub fn lint_only(
&locator,
&directives,
),
has_syntax_error: !parsed.is_valid() || !parsed.unsupported_syntax_errors().is_empty(),
has_syntax_error: !parsed.has_no_errors(),
}
}

Expand Down Expand Up @@ -546,7 +548,7 @@ pub fn lint_fix<'a>(
);

if iterations == 0 {
is_valid_syntax = parsed.is_valid() && parsed.unsupported_syntax_errors().is_empty();
is_valid_syntax = parsed.has_no_errors();
} else {
// If the source code was parseable on the first pass, but is no
// longer parseable on a subsequent pass, then we've introduced a
Expand Down
10 changes: 10 additions & 0 deletions crates/ruff_python_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,20 @@ impl<T> Parsed<T> {
///
/// Note that this does not include version-related
/// [`unsupported_syntax_errors`](Parsed::unsupported_syntax_errors).
///
/// See [`has_no_errors`](Parsed::has_no_errors) for a version that takes these into account.
pub fn is_valid(&self) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not specifically related to this PR but it seems a good opportunity. How about:

  • Add a method has_invalid_syntax or is_invalid_syntax as the opposite of is_valid (we could also rename is_valid to has_valid_syntax or is_valid_syntax. It's still somewhat ambigious by what it mean but I think it works well with unsupported syntax (which technically is valid, it's just unsupported)
  • Rename the new method to has_syntax_errors and has_no_syntax_errors?

self.errors.is_empty()
}

/// Returns `true` if the parsed source code does not contain any [`ParseError`]s *or*
/// [`UnsupportedSyntaxError`]s.
///
/// See [`Parsed::is_valid`] for a version specific to [`ParseError`]s.
pub fn has_no_errors(&self) -> bool {
self.is_valid() && self.unsupported_syntax_errors.is_empty()
}

/// Returns the [`Parsed`] output as a [`Result`], returning [`Ok`] if it has no syntax errors,
/// or [`Err`] containing the first [`ParseError`] encountered.
///
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_python_parser/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn test_valid_syntax(input_path: &Path) {
});
let parsed = parse_unchecked(&source, options);

let is_valid = parsed.is_valid() && parsed.unsupported_syntax_errors().is_empty();
let is_valid = parsed.has_no_errors();

if !is_valid {
let line_index = LineIndex::from_source_text(&source);
Expand Down Expand Up @@ -101,7 +101,7 @@ fn test_invalid_syntax(input_path: &Path) {
});
let parsed = parse_unchecked(&source, options);

let is_valid = parsed.is_valid() && parsed.unsupported_syntax_errors().is_empty();
let is_valid = parsed.has_no_errors();

assert!(
!is_valid,
Expand Down
Loading