mirror of
https://github.com/astral-sh/ruff.git
synced 2025-12-04 01:36:46 +00:00
Use const-singleton helpers in more rules (#5142)
This commit is contained in:
parent
fab2a4adf7
commit
5526699535
11 changed files with 37 additions and 107 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
use rustpython_parser::ast::{self, Constant, Expr, Keyword, Ranged};
|
use rustpython_parser::ast::{Expr, Keyword, Ranged};
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers::SimpleCallArgs;
|
use ruff_python_ast::helpers::{is_const_false, SimpleCallArgs};
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
|
||||||
|
|
@ -60,11 +60,7 @@ pub(crate) fn request_with_no_cert_validation(
|
||||||
{
|
{
|
||||||
let call_args = SimpleCallArgs::new(args, keywords);
|
let call_args = SimpleCallArgs::new(args, keywords);
|
||||||
if let Some(verify_arg) = call_args.keyword_argument("verify") {
|
if let Some(verify_arg) = call_args.keyword_argument("verify") {
|
||||||
if let Expr::Constant(ast::ExprConstant {
|
if is_const_false(verify_arg) {
|
||||||
value: Constant::Bool(false),
|
|
||||||
..
|
|
||||||
}) = &verify_arg
|
|
||||||
{
|
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
RequestWithNoCertValidation {
|
RequestWithNoCertValidation {
|
||||||
string: target.to_string(),
|
string: target.to_string(),
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
use ruff_text_size::TextRange;
|
use ruff_text_size::TextRange;
|
||||||
use rustpython_parser::ast::{self, Constant, Expr, ExprContext, Ranged, Stmt};
|
use rustpython_parser::ast::{self, Expr, ExprContext, Ranged, Stmt};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
use ruff_python_ast::helpers::is_const_false;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::registry::AsRule;
|
use crate::registry::AsRule;
|
||||||
|
|
@ -44,12 +45,9 @@ fn assertion_error(msg: Option<&Expr>) -> Stmt {
|
||||||
|
|
||||||
/// B011
|
/// B011
|
||||||
pub(crate) fn assert_false(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg: Option<&Expr>) {
|
pub(crate) fn assert_false(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg: Option<&Expr>) {
|
||||||
let Expr::Constant(ast::ExprConstant {
|
if !is_const_false(test) {
|
||||||
value: Constant::Bool(false),
|
|
||||||
..
|
|
||||||
} )= &test else {
|
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
|
|
||||||
let mut diagnostic = Diagnostic::new(AssertFalse, test.range());
|
let mut diagnostic = Diagnostic::new(AssertFalse, test.range());
|
||||||
if checker.patch(diagnostic.kind.rule()) {
|
if checker.patch(diagnostic.kind.rule()) {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
use rustpython_parser::ast::{self, Constant, Expr, Ranged, Stmt};
|
use rustpython_parser::ast::{self, Expr, Ranged, Stmt};
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
use ruff_python_ast::helpers::is_const_true;
|
||||||
use ruff_python_semantic::SemanticModel;
|
use ruff_python_semantic::SemanticModel;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
|
@ -112,9 +113,9 @@ fn is_model_abstract(body: &[Stmt]) -> bool {
|
||||||
if id != "abstract" {
|
if id != "abstract" {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let Expr::Constant(ast::ExprConstant{value: Constant::Bool(true), ..}) = value.as_ref() else {
|
if !is_const_true(value) {
|
||||||
continue;
|
continue;
|
||||||
};
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
use rustpython_parser::ast::Constant::Bool;
|
|
||||||
use rustpython_parser::ast::{self, Expr, Ranged, Stmt};
|
use rustpython_parser::ast::{self, Expr, Ranged, Stmt};
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
use ruff_python_ast::helpers::is_const_true;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
|
||||||
|
|
@ -96,12 +96,12 @@ fn is_nullable_field<'a>(checker: &'a Checker, value: &'a Expr) -> Option<&'a st
|
||||||
let mut blank_key = false;
|
let mut blank_key = false;
|
||||||
let mut unique_key = false;
|
let mut unique_key = false;
|
||||||
for keyword in keywords.iter() {
|
for keyword in keywords.iter() {
|
||||||
let Expr::Constant(ast::ExprConstant {value: Bool(true), ..}) = &keyword.value else {
|
|
||||||
continue
|
|
||||||
};
|
|
||||||
let Some(argument) = &keyword.arg else {
|
let Some(argument) = &keyword.arg else {
|
||||||
continue
|
continue
|
||||||
};
|
};
|
||||||
|
if !is_const_true(&keyword.value) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
match argument.as_str() {
|
match argument.as_str() {
|
||||||
"blank" => blank_key = true,
|
"blank" => blank_key = true,
|
||||||
"null" => null_key = true,
|
"null" => null_key = true,
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
|
|
||||||
use ruff_text_size::{TextRange, TextSize};
|
use ruff_text_size::{TextRange, TextSize};
|
||||||
use rustpython_parser::ast::{self, Constant, Expr, Ranged, Stmt};
|
use rustpython_parser::ast::{self, Expr, Ranged, Stmt};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Violation};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Violation};
|
||||||
use ruff_diagnostics::{Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers::elif_else_range;
|
|
||||||
use ruff_python_ast::helpers::is_const_none;
|
use ruff_python_ast::helpers::is_const_none;
|
||||||
|
use ruff_python_ast::helpers::{elif_else_range, is_const_false, is_const_true};
|
||||||
use ruff_python_ast::visitor::Visitor;
|
use ruff_python_ast::visitor::Visitor;
|
||||||
use ruff_python_ast::whitespace::indentation;
|
use ruff_python_ast::whitespace::indentation;
|
||||||
use ruff_python_semantic::SemanticModel;
|
use ruff_python_semantic::SemanticModel;
|
||||||
|
|
@ -339,13 +339,7 @@ fn unnecessary_return_none(checker: &mut Checker, stack: &Stack) {
|
||||||
let Some(expr) = stmt.value.as_deref() else {
|
let Some(expr) = stmt.value.as_deref() else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
if !matches!(
|
if !is_const_none(expr) {
|
||||||
expr,
|
|
||||||
Expr::Constant(ast::ExprConstant {
|
|
||||||
value: Constant::None,
|
|
||||||
..
|
|
||||||
})
|
|
||||||
) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let mut diagnostic = Diagnostic::new(UnnecessaryReturnNone, stmt.range);
|
let mut diagnostic = Diagnostic::new(UnnecessaryReturnNone, stmt.range);
|
||||||
|
|
@ -433,22 +427,8 @@ fn implicit_return(checker: &mut Checker, stmt: &Stmt) {
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::Assert(ast::StmtAssert { test, .. })
|
Stmt::Assert(ast::StmtAssert { test, .. }) if is_const_false(test) => {}
|
||||||
if matches!(
|
Stmt::While(ast::StmtWhile { test, .. }) if is_const_true(test) => {}
|
||||||
test.as_ref(),
|
|
||||||
Expr::Constant(ast::ExprConstant {
|
|
||||||
value: Constant::Bool(false),
|
|
||||||
..
|
|
||||||
})
|
|
||||||
) => {}
|
|
||||||
Stmt::While(ast::StmtWhile { test, .. })
|
|
||||||
if matches!(
|
|
||||||
test.as_ref(),
|
|
||||||
Expr::Constant(ast::ExprConstant {
|
|
||||||
value: Constant::Bool(true),
|
|
||||||
..
|
|
||||||
})
|
|
||||||
) => {}
|
|
||||||
Stmt::For(ast::StmtFor { orelse, .. })
|
Stmt::For(ast::StmtFor { orelse, .. })
|
||||||
| Stmt::AsyncFor(ast::StmtAsyncFor { orelse, .. })
|
| Stmt::AsyncFor(ast::StmtAsyncFor { orelse, .. })
|
||||||
| Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
| Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ use rustpython_parser::ast::{self, Constant, Expr, Ranged};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, AutofixKind, Diagnostic, Edit, Fix, Violation};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, AutofixKind, Diagnostic, Edit, Fix, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
use ruff_python_ast::helpers::is_const_none;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::registry::AsRule;
|
use crate::registry::AsRule;
|
||||||
|
|
@ -208,15 +209,9 @@ pub(crate) fn dict_get_with_none_default(checker: &mut Checker, expr: &Expr) {
|
||||||
let Some(default) = args.get(1) else {
|
let Some(default) = args.get(1) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
if !matches!(
|
if !is_const_none(default) {
|
||||||
default,
|
|
||||||
Expr::Constant(ast::ExprConstant {
|
|
||||||
value: Constant::None,
|
|
||||||
..
|
|
||||||
})
|
|
||||||
) {
|
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
|
|
||||||
let expected = format!(
|
let expected = format!(
|
||||||
"{}({})",
|
"{}({})",
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ use rustpython_parser::ast::{self, Cmpop, Constant, Expr, Ranged};
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::helpers;
|
use ruff_python_ast::helpers;
|
||||||
|
use ruff_python_ast::helpers::is_const_none;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::registry::AsRule;
|
use crate::registry::AsRule;
|
||||||
|
|
@ -153,16 +154,7 @@ pub(crate) fn literal_comparisons(
|
||||||
|
|
||||||
if !helpers::is_constant_non_singleton(next) {
|
if !helpers::is_constant_non_singleton(next) {
|
||||||
if let Some(op) = EqCmpop::try_from(*op) {
|
if let Some(op) = EqCmpop::try_from(*op) {
|
||||||
if check_none_comparisons
|
if check_none_comparisons && is_const_none(comparator) {
|
||||||
&& matches!(
|
|
||||||
comparator,
|
|
||||||
Expr::Constant(ast::ExprConstant {
|
|
||||||
value: Constant::None,
|
|
||||||
kind: None,
|
|
||||||
range: _
|
|
||||||
})
|
|
||||||
)
|
|
||||||
{
|
|
||||||
match op {
|
match op {
|
||||||
EqCmpop::Eq => {
|
EqCmpop::Eq => {
|
||||||
let diagnostic = Diagnostic::new(NoneComparison(op), comparator.range());
|
let diagnostic = Diagnostic::new(NoneComparison(op), comparator.range());
|
||||||
|
|
@ -223,16 +215,7 @@ pub(crate) fn literal_comparisons(
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(op) = EqCmpop::try_from(*op) {
|
if let Some(op) = EqCmpop::try_from(*op) {
|
||||||
if check_none_comparisons
|
if check_none_comparisons && is_const_none(next) {
|
||||||
&& matches!(
|
|
||||||
next,
|
|
||||||
Expr::Constant(ast::ExprConstant {
|
|
||||||
value: Constant::None,
|
|
||||||
kind: None,
|
|
||||||
range: _
|
|
||||||
})
|
|
||||||
)
|
|
||||||
{
|
|
||||||
match op {
|
match op {
|
||||||
EqCmpop::Eq => {
|
EqCmpop::Eq => {
|
||||||
let diagnostic = Diagnostic::new(NoneComparison(op), next.range());
|
let diagnostic = Diagnostic::new(NoneComparison(op), next.range());
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
use rustpython_parser::ast::{self, Constant, Expr, Ranged, Stmt};
|
use rustpython_parser::ast::{self, Ranged, Stmt};
|
||||||
|
|
||||||
use ruff_diagnostics::{Diagnostic, Violation};
|
use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
use ruff_python_ast::helpers::is_const_none;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::rules::pylint::helpers::in_dunder_init;
|
use crate::rules::pylint::helpers::in_dunder_init;
|
||||||
|
|
@ -46,13 +47,7 @@ impl Violation for ReturnInInit {
|
||||||
pub(crate) fn return_in_init(checker: &mut Checker, stmt: &Stmt) {
|
pub(crate) fn return_in_init(checker: &mut Checker, stmt: &Stmt) {
|
||||||
if let Stmt::Return(ast::StmtReturn { value, range: _ }) = stmt {
|
if let Stmt::Return(ast::StmtReturn { value, range: _ }) = stmt {
|
||||||
if let Some(expr) = value {
|
if let Some(expr) = value {
|
||||||
if matches!(
|
if is_const_none(expr) {
|
||||||
expr.as_ref(),
|
|
||||||
Expr::Constant(ast::ExprConstant {
|
|
||||||
value: Constant::None,
|
|
||||||
..
|
|
||||||
})
|
|
||||||
) {
|
|
||||||
// Explicit `return None`.
|
// Explicit `return None`.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
use ruff_text_size::TextRange;
|
use ruff_text_size::TextRange;
|
||||||
use rustpython_parser::ast::{self, Constant, Decorator, Expr, Keyword, Ranged};
|
use rustpython_parser::ast::{self, Decorator, Expr, Keyword, Ranged};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
use ruff_python_ast::helpers::is_const_none;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::importer::ImportRequest;
|
use crate::importer::ImportRequest;
|
||||||
|
|
@ -82,16 +83,7 @@ pub(crate) fn lru_cache_with_maxsize_none(checker: &mut Checker, decorator_list:
|
||||||
value,
|
value,
|
||||||
range: _,
|
range: _,
|
||||||
} = &keywords[0];
|
} = &keywords[0];
|
||||||
if arg.as_ref().map_or(false, |arg| arg == "maxsize")
|
if arg.as_ref().map_or(false, |arg| arg == "maxsize") && is_const_none(value) {
|
||||||
&& matches!(
|
|
||||||
value,
|
|
||||||
Expr::Constant(ast::ExprConstant {
|
|
||||||
value: Constant::None,
|
|
||||||
kind: None,
|
|
||||||
range: _,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
{
|
|
||||||
let mut diagnostic = Diagnostic::new(
|
let mut diagnostic = Diagnostic::new(
|
||||||
LRUCacheWithMaxsizeNone,
|
LRUCacheWithMaxsizeNone,
|
||||||
TextRange::new(func.end(), decorator.end()),
|
TextRange::new(func.end(), decorator.end()),
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ use rustpython_parser::ast::{self, Arguments, Constant, Expr, Operator, Ranged};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
use ruff_python_ast::helpers::is_const_none;
|
||||||
use ruff_python_semantic::SemanticModel;
|
use ruff_python_semantic::SemanticModel;
|
||||||
use ruff_text_size::TextRange;
|
use ruff_text_size::TextRange;
|
||||||
|
|
||||||
|
|
@ -320,13 +321,7 @@ pub(crate) fn implicit_optional(checker: &mut Checker, arguments: &Arguments) {
|
||||||
.zip(arguments.defaults.iter().rev()),
|
.zip(arguments.defaults.iter().rev()),
|
||||||
);
|
);
|
||||||
for (arg, default) in arguments_with_defaults {
|
for (arg, default) in arguments_with_defaults {
|
||||||
if !matches!(
|
if !is_const_none(default) {
|
||||||
default,
|
|
||||||
Expr::Constant(ast::ExprConstant {
|
|
||||||
value: Constant::None,
|
|
||||||
..
|
|
||||||
}),
|
|
||||||
) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let Some(annotation) = &arg.annotation else {
|
let Some(annotation) = &arg.annotation else {
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
//! Analysis rules for the `typing` module.
|
//! Analysis rules for the `typing` module.
|
||||||
|
|
||||||
|
use num_traits::identities::Zero;
|
||||||
use rustpython_parser::ast::{self, Constant, Expr, Operator};
|
use rustpython_parser::ast::{self, Constant, Expr, Operator};
|
||||||
|
|
||||||
use num_traits::identities::Zero;
|
|
||||||
use ruff_python_ast::call_path::{from_qualified_name, from_unqualified_name, CallPath};
|
use ruff_python_ast::call_path::{from_qualified_name, from_unqualified_name, CallPath};
|
||||||
|
use ruff_python_ast::helpers::is_const_false;
|
||||||
use ruff_python_stdlib::typing::{
|
use ruff_python_stdlib::typing::{
|
||||||
IMMUTABLE_GENERIC_TYPES, IMMUTABLE_TYPES, PEP_585_GENERICS, PEP_593_SUBSCRIPTS, SUBSCRIPTS,
|
IMMUTABLE_GENERIC_TYPES, IMMUTABLE_TYPES, PEP_585_GENERICS, PEP_593_SUBSCRIPTS, SUBSCRIPTS,
|
||||||
};
|
};
|
||||||
|
|
@ -267,13 +268,7 @@ pub fn is_type_checking_block(stmt: &ast::StmtIf, semantic: &SemanticModel) -> b
|
||||||
let ast::StmtIf { test, .. } = stmt;
|
let ast::StmtIf { test, .. } = stmt;
|
||||||
|
|
||||||
// Ex) `if False:`
|
// Ex) `if False:`
|
||||||
if matches!(
|
if is_const_false(test) {
|
||||||
test.as_ref(),
|
|
||||||
Expr::Constant(ast::ExprConstant {
|
|
||||||
value: Constant::Bool(false),
|
|
||||||
..
|
|
||||||
})
|
|
||||||
) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue