Avoid autofixing some PT violations when comments are present (#3198)

This commit is contained in:
Charlie Marsh 2023-02-23 22:48:41 -05:00 committed by GitHub
parent 159422071e
commit f38624824d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 7 deletions

View File

@ -23,7 +23,7 @@ def test_error():
""" """
# recursive case # recursive case
assert not (a or not (b or c)) # note that we only reduce once here assert not (a or not (b or c))
assert not (a or not (b and c)) assert not (a or not (b and c))
# detected, but no autofix for messages # detected, but no autofix for messages

View File

@ -12,7 +12,7 @@ use rustpython_parser::ast::{
use ruff_macros::{define_violation, derive_message_formats}; use ruff_macros::{define_violation, derive_message_formats};
use crate::ast::helpers::unparse_stmt; use crate::ast::helpers::{has_comments_in, unparse_stmt};
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::ast::visitor::Visitor; use crate::ast::visitor::Visitor;
use crate::ast::{visitor, whitespace}; use crate::ast::{visitor, whitespace};
@ -187,7 +187,7 @@ fn check_assert_in_except(name: &str, body: &[Stmt]) -> Vec<Diagnostic> {
/// PT009 /// PT009
pub fn unittest_assertion( pub fn unittest_assertion(
checker: &Checker, checker: &Checker,
call: &Expr, expr: &Expr,
func: &Expr, func: &Expr,
args: &[Expr], args: &[Expr],
keywords: &[Keyword], keywords: &[Keyword],
@ -198,7 +198,8 @@ pub fn unittest_assertion(
// We're converting an expression to a statement, so avoid applying the fix if // We're converting an expression to a statement, so avoid applying the fix if
// the assertion is part of a larger expression. // the assertion is part of a larger expression.
let fixable = checker.current_expr_parent().is_none() let fixable = checker.current_expr_parent().is_none()
&& matches!(checker.current_stmt().node, StmtKind::Expr { .. }); && matches!(checker.current_stmt().node, StmtKind::Expr { .. })
&& !has_comments_in(Range::from_located(expr), checker.locator);
let mut diagnostic = Diagnostic::new( let mut diagnostic = Diagnostic::new(
UnittestAssertion { UnittestAssertion {
assertion: unittest_assert.to_string(), assertion: unittest_assert.to_string(),
@ -210,8 +211,8 @@ pub fn unittest_assertion(
if let Ok(stmt) = unittest_assert.generate_assert(args, keywords) { if let Ok(stmt) = unittest_assert.generate_assert(args, keywords) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
unparse_stmt(&stmt, checker.stylist), unparse_stmt(&stmt, checker.stylist),
call.location, expr.location,
call.end_location.unwrap(), expr.end_location.unwrap(),
)); ));
} }
} }
@ -434,7 +435,9 @@ fn fix_composite_condition(stmt: &Stmt, locator: &Locator, stylist: &Stylist) ->
pub fn composite_condition(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg: Option<&Expr>) { pub fn composite_condition(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg: Option<&Expr>) {
let composite = is_composite_condition(test); let composite = is_composite_condition(test);
if matches!(composite, CompositionKind::Simple | CompositionKind::Mixed) { if matches!(composite, CompositionKind::Simple | CompositionKind::Mixed) {
let fixable = matches!(composite, CompositionKind::Simple) && msg.is_none(); let fixable = matches!(composite, CompositionKind::Simple)
&& msg.is_none()
&& !has_comments_in(Range::from_located(stmt), checker.locator);
let mut diagnostic = let mut diagnostic =
Diagnostic::new(CompositeAssertion { fixable }, Range::from_located(stmt)); Diagnostic::new(CompositeAssertion { fixable }, Range::from_located(stmt));
if fixable && checker.patch(diagnostic.kind.rule()) { if fixable && checker.patch(diagnostic.kind.rule()) {