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 7 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
10 changes: 5 additions & 5 deletions crates/ruff_db/src/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ mod tests {

let parsed = parsed_module(&db, file);

assert!(parsed.is_valid());
assert!(parsed.has_valid_syntax());

Ok(())
}
Expand All @@ -118,7 +118,7 @@ mod tests {

let parsed = parsed_module(&db, file);

assert!(parsed.is_valid());
assert!(parsed.has_valid_syntax());

Ok(())
}
Expand All @@ -134,7 +134,7 @@ mod tests {

let parsed = parsed_module(&db, virtual_file.file());

assert!(parsed.is_valid());
assert!(parsed.has_valid_syntax());

Ok(())
}
Expand All @@ -150,7 +150,7 @@ mod tests {

let parsed = parsed_module(&db, virtual_file.file());

assert!(parsed.is_valid());
assert!(parsed.has_valid_syntax());

Ok(())
}
Expand Down Expand Up @@ -181,6 +181,6 @@ else:

let parsed = parsed_module(&db, file);

assert!(parsed.is_valid());
assert!(parsed.has_valid_syntax());
}
}
12 changes: 7 additions & 5 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 @@ -133,7 +135,7 @@ pub fn check_path(
}

// Run the AST-based rules only if there are no syntax errors.
if parsed.is_valid() {
if parsed.has_valid_syntax() {
let use_ast = settings
.rules
.iter_enabled()
Expand Down Expand Up @@ -283,7 +285,7 @@ pub fn check_path(
locator,
comment_ranges,
&directives.noqa_line_for,
parsed.is_valid(),
parsed.has_valid_syntax(),
&per_file_ignores,
settings,
);
Expand All @@ -294,7 +296,7 @@ pub fn check_path(
}
}

if parsed.is_valid() {
if parsed.has_valid_syntax() {
// Remove fixes for any rules marked as unfixable.
for diagnostic in &mut diagnostics {
if !settings.rules.should_fix(diagnostic.kind.rule()) {
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_syntax_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_syntax_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
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub(crate) fn test_contents<'a>(
target_version,
);

let source_has_errors = !parsed.is_valid();
let source_has_errors = parsed.has_invalid_syntax();

// Detect fixes that don't converge after multiple iterations.
let mut iterations = 0;
Expand Down Expand Up @@ -207,7 +207,7 @@ pub(crate) fn test_contents<'a>(
target_version,
);

if !parsed.is_valid() && !source_has_errors {
if parsed.has_invalid_syntax() && !source_has_errors {
// Previous fix introduced a syntax error, abort
let fixes = print_diagnostics(diagnostics, path, source_kind);
let syntax_errors =
Expand Down
36 changes: 31 additions & 5 deletions crates/ruff_python_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,19 +344,45 @@ impl<T> Parsed<T> {

/// Returns `true` if the parsed source code is valid i.e., it has no syntax errors.
///
/// Note that this does not include version-related
/// [`unsupported_syntax_errors`](Parsed::unsupported_syntax_errors).
pub fn is_valid(&self) -> bool {
/// Note that this does not include version-related [`Parsed::unsupported_syntax_errors`].
///
/// See [`Parsed::has_no_syntax_errors`] for a version that takes these into account.
pub fn has_valid_syntax(&self) -> bool {
self.errors.is_empty()
}

/// Returns `true` if the parsed source code is invalid i.e., it has syntax errors.
Copy link
Member

@MichaReiser MichaReiser Mar 3, 2025

Choose a reason for hiding this comment

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

I think i'd prefer to use the term parse errors or a more detailed description over using syntax errors. The description otherwise doesn't make it clear how it is different from has_syntax_erros.

But it's tricky with all those being syntax errors.

Another option is to rename unsupported_syntax_errors to has_unsupported_syntax (which would match has_invalid_syntax) or has_no_unsupported_syntax.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh good catch. I can even link to the ParseError and UnsupportedSyntaxError types like I did in the new methods.

///
/// Note that this does not include version-related [`Parsed::unsupported_syntax_errors`].
///
/// See [`Parsed::has_no_syntax_errors`] for a version that takes these into account.
pub fn has_invalid_syntax(&self) -> bool {
!self.has_valid_syntax()
}

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

/// Returns `true` if the parsed source code contains any [`ParseError`]s *or*
/// [`UnsupportedSyntaxError`]s.
///
/// See [`Parsed::has_invalid_syntax`] for a version specific to [`ParseError`]s.
pub fn has_syntax_errors(&self) -> bool {
!self.has_no_syntax_errors()
}

/// Returns the [`Parsed`] output as a [`Result`], returning [`Ok`] if it has no syntax errors,
/// or [`Err`] containing the first [`ParseError`] encountered.
///
/// Note that any [`unsupported_syntax_errors`](Parsed::unsupported_syntax_errors) will not
/// cause [`Err`] to be returned.
pub fn as_result(&self) -> Result<&Parsed<T>, &[ParseError]> {
if self.is_valid() {
if self.has_valid_syntax() {
Ok(self)
} else {
Err(&self.errors)
Expand All @@ -369,7 +395,7 @@ impl<T> Parsed<T> {
/// Note that any [`unsupported_syntax_errors`](Parsed::unsupported_syntax_errors) will not
/// cause [`Err`] to be returned.
pub(crate) fn into_result(self) -> Result<Parsed<T>, ParseError> {
if self.is_valid() {
if self.has_valid_syntax() {
Ok(self)
} else {
Err(self.into_errors().into_iter().next().unwrap())
Expand Down
10 changes: 3 additions & 7 deletions crates/ruff_python_parser/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +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();

if !is_valid {
if parsed.has_syntax_errors() {
let line_index = LineIndex::from_source_text(&source);
let source_code = SourceCode::new(&source, &line_index);

Expand Down Expand Up @@ -101,10 +99,8 @@ 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();

assert!(
!is_valid,
parsed.has_syntax_errors(),
"{input_path:?}: Expected parser to generate at least one syntax error for a program containing syntax errors."
);

Expand Down Expand Up @@ -224,7 +220,7 @@ f'{foo!r'
println!("AST:\n----\n{:#?}", parsed.syntax());
println!("Tokens:\n-------\n{:#?}", parsed.tokens());

if !parsed.is_valid() {
if parsed.has_invalid_syntax() {
println!("Errors:\n-------");

let line_index = LineIndex::from_source_text(source);
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/red_knot_check_invalid_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn do_fuzz(case: &[u8]) -> Corpus {
};

let parsed = parse_unchecked(code, ParseOptions::from(Mode::Module));
if parsed.is_valid() {
if parsed.has_valid_syntax() {
return Corpus::Reject;
}

Expand Down
Loading