mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-16 08:30:30 +00:00
refactor(ruff_python_ast): Split get_argument
(#3478)
This commit is contained in:
parent
b540407b74
commit
685c242761
13 changed files with 39 additions and 42 deletions
|
@ -108,7 +108,7 @@ pub fn bad_file_permissions(
|
||||||
.map_or(false, |call_path| call_path.as_slice() == ["os", "chmod"])
|
.map_or(false, |call_path| call_path.as_slice() == ["os", "chmod"])
|
||||||
{
|
{
|
||||||
let call_args = SimpleCallArgs::new(args, keywords);
|
let call_args = SimpleCallArgs::new(args, keywords);
|
||||||
if let Some(mode_arg) = call_args.get_argument("mode", Some(1)) {
|
if let Some(mode_arg) = call_args.argument("mode", 1) {
|
||||||
if let Some(int_value) = get_int_value(mode_arg) {
|
if let Some(int_value) = get_int_value(mode_arg) {
|
||||||
if (int_value & WRITE_WORLD > 0) || (int_value & EXECUTE_GROUP > 0) {
|
if (int_value & WRITE_WORLD > 0) || (int_value & EXECUTE_GROUP > 0) {
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
|
|
|
@ -25,7 +25,7 @@ impl Violation for HashlibInsecureHashFunction {
|
||||||
const WEAK_HASHES: [&str; 4] = ["md4", "md5", "sha", "sha1"];
|
const WEAK_HASHES: [&str; 4] = ["md4", "md5", "sha", "sha1"];
|
||||||
|
|
||||||
fn is_used_for_security(call_args: &SimpleCallArgs) -> bool {
|
fn is_used_for_security(call_args: &SimpleCallArgs) -> bool {
|
||||||
match call_args.get_argument("usedforsecurity", None) {
|
match call_args.keyword_argument("usedforsecurity") {
|
||||||
Some(expr) => !matches!(
|
Some(expr) => !matches!(
|
||||||
&expr.node,
|
&expr.node,
|
||||||
ExprKind::Constant {
|
ExprKind::Constant {
|
||||||
|
@ -67,7 +67,7 @@ pub fn hashlib_insecure_hash_functions(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(name_arg) = call_args.get_argument("name", Some(0)) {
|
if let Some(name_arg) = call_args.argument("name", 0) {
|
||||||
if let Some(hash_func_name) = string_literal(name_arg) {
|
if let Some(hash_func_name) = string_literal(name_arg) {
|
||||||
if WEAK_HASHES.contains(&hash_func_name.to_lowercase().as_str()) {
|
if WEAK_HASHES.contains(&hash_func_name.to_lowercase().as_str()) {
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
|
|
|
@ -46,7 +46,7 @@ pub fn jinja2_autoescape_false(
|
||||||
{
|
{
|
||||||
let call_args = SimpleCallArgs::new(args, keywords);
|
let call_args = SimpleCallArgs::new(args, keywords);
|
||||||
|
|
||||||
if let Some(autoescape_arg) = call_args.get_argument("autoescape", None) {
|
if let Some(autoescape_arg) = call_args.keyword_argument("autoescape") {
|
||||||
match &autoescape_arg.node {
|
match &autoescape_arg.node {
|
||||||
ExprKind::Constant {
|
ExprKind::Constant {
|
||||||
value: Constant::Bool(true),
|
value: Constant::Bool(true),
|
||||||
|
|
|
@ -33,7 +33,7 @@ pub fn logging_config_insecure_listen(
|
||||||
{
|
{
|
||||||
let call_args = SimpleCallArgs::new(args, keywords);
|
let call_args = SimpleCallArgs::new(args, keywords);
|
||||||
|
|
||||||
if call_args.get_argument("verify", None).is_none() {
|
if call_args.keyword_argument("verify").is_none() {
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
LoggingConfigInsecureListen,
|
LoggingConfigInsecureListen,
|
||||||
Range::from(func),
|
Range::from(func),
|
||||||
|
|
|
@ -56,7 +56,7 @@ pub fn request_with_no_cert_validation(
|
||||||
None
|
None
|
||||||
}) {
|
}) {
|
||||||
let call_args = SimpleCallArgs::new(args, keywords);
|
let call_args = SimpleCallArgs::new(args, keywords);
|
||||||
if let Some(verify_arg) = call_args.get_argument("verify", None) {
|
if let Some(verify_arg) = call_args.keyword_argument("verify") {
|
||||||
if let ExprKind::Constant {
|
if let ExprKind::Constant {
|
||||||
value: Constant::Bool(false),
|
value: Constant::Bool(false),
|
||||||
..
|
..
|
||||||
|
|
|
@ -44,7 +44,7 @@ pub fn request_without_timeout(
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
let call_args = SimpleCallArgs::new(args, keywords);
|
let call_args = SimpleCallArgs::new(args, keywords);
|
||||||
if let Some(timeout_arg) = call_args.get_argument("timeout", None) {
|
if let Some(timeout_arg) = call_args.keyword_argument("timeout") {
|
||||||
if let Some(timeout) = match &timeout_arg.node {
|
if let Some(timeout) = match &timeout_arg.node {
|
||||||
ExprKind::Constant {
|
ExprKind::Constant {
|
||||||
value: value @ Constant::None,
|
value: value @ Constant::None,
|
||||||
|
|
|
@ -33,7 +33,7 @@ pub fn snmp_insecure_version(
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
let call_args = SimpleCallArgs::new(args, keywords);
|
let call_args = SimpleCallArgs::new(args, keywords);
|
||||||
if let Some(mp_model_arg) = call_args.get_argument("mpModel", None) {
|
if let Some(mp_model_arg) = call_args.keyword_argument("mpModel") {
|
||||||
if let ExprKind::Constant {
|
if let ExprKind::Constant {
|
||||||
value: Constant::Int(value),
|
value: Constant::Int(value),
|
||||||
..
|
..
|
||||||
|
|
|
@ -39,7 +39,7 @@ pub fn unsafe_yaml_load(checker: &mut Checker, func: &Expr, args: &[Expr], keywo
|
||||||
.map_or(false, |call_path| call_path.as_slice() == ["yaml", "load"])
|
.map_or(false, |call_path| call_path.as_slice() == ["yaml", "load"])
|
||||||
{
|
{
|
||||||
let call_args = SimpleCallArgs::new(args, keywords);
|
let call_args = SimpleCallArgs::new(args, keywords);
|
||||||
if let Some(loader_arg) = call_args.get_argument("Loader", Some(1)) {
|
if let Some(loader_arg) = call_args.argument("Loader", 1) {
|
||||||
if !checker
|
if !checker
|
||||||
.ctx
|
.ctx
|
||||||
.resolve_call_path(loader_arg)
|
.resolve_call_path(loader_arg)
|
||||||
|
|
|
@ -146,7 +146,7 @@ pub fn logging_call(checker: &mut Checker, func: &Expr, args: &[Expr], keywords:
|
||||||
);
|
);
|
||||||
|
|
||||||
// G001 - G004
|
// G001 - G004
|
||||||
if let Some(format_arg) = call_args.get_argument("msg", Some(0)) {
|
if let Some(format_arg) = call_args.argument("msg", 0) {
|
||||||
check_msg(checker, format_arg);
|
check_msg(checker, format_arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ impl Violation for FailWithoutMessage {
|
||||||
pub fn fail_call(checker: &mut Checker, func: &Expr, args: &[Expr], keywords: &[Keyword]) {
|
pub fn fail_call(checker: &mut Checker, func: &Expr, args: &[Expr], keywords: &[Keyword]) {
|
||||||
if is_pytest_fail(func, checker) {
|
if is_pytest_fail(func, checker) {
|
||||||
let call_args = SimpleCallArgs::new(args, keywords);
|
let call_args = SimpleCallArgs::new(args, keywords);
|
||||||
let msg = call_args.get_argument("msg", Some(0));
|
let msg = call_args.argument("msg", 0);
|
||||||
|
|
||||||
if let Some(msg) = msg {
|
if let Some(msg) = msg {
|
||||||
if is_empty_or_null_string(msg) {
|
if is_empty_or_null_string(msg) {
|
||||||
|
|
|
@ -68,11 +68,11 @@ fn check_patch_call(
|
||||||
new_arg_number: usize,
|
new_arg_number: usize,
|
||||||
) -> Option<Diagnostic> {
|
) -> Option<Diagnostic> {
|
||||||
let simple_args = SimpleCallArgs::new(args, keywords);
|
let simple_args = SimpleCallArgs::new(args, keywords);
|
||||||
if simple_args.get_argument("return_value", None).is_some() {
|
if simple_args.keyword_argument("return_value").is_some() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(new_arg) = simple_args.get_argument("new", Some(new_arg_number)) {
|
if let Some(new_arg) = simple_args.argument("new", new_arg_number) {
|
||||||
if let ExprKind::Lambda { args, body } = &new_arg.node {
|
if let ExprKind::Lambda { args, body } = &new_arg.node {
|
||||||
// Walk the lambda body.
|
// Walk the lambda body.
|
||||||
let mut visitor = LambdaBodyVisitor {
|
let mut visitor = LambdaBodyVisitor {
|
||||||
|
|
|
@ -107,7 +107,7 @@ pub fn logging_call(checker: &mut Checker, func: &Expr, args: &[Expr], keywords:
|
||||||
if let ExprKind::Attribute { attr, .. } = &func.node {
|
if let ExprKind::Attribute { attr, .. } = &func.node {
|
||||||
if LoggingLevel::from_attribute(attr.as_str()).is_some() {
|
if LoggingLevel::from_attribute(attr.as_str()).is_some() {
|
||||||
let call_args = SimpleCallArgs::new(args, keywords);
|
let call_args = SimpleCallArgs::new(args, keywords);
|
||||||
if let Some(msg) = call_args.get_argument("msg", Some(0)) {
|
if let Some(msg) = call_args.argument("msg", 0) {
|
||||||
if let ExprKind::Constant {
|
if let ExprKind::Constant {
|
||||||
value: Constant::Str(value),
|
value: Constant::Str(value),
|
||||||
..
|
..
|
||||||
|
|
|
@ -1213,42 +1213,39 @@ pub struct SimpleCallArgs<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> SimpleCallArgs<'a> {
|
impl<'a> SimpleCallArgs<'a> {
|
||||||
pub fn new(args: &'a [Expr], keywords: &'a [Keyword]) -> Self {
|
pub fn new(
|
||||||
let mut result = SimpleCallArgs::default();
|
args: impl IntoIterator<Item = &'a Expr>,
|
||||||
|
keywords: impl IntoIterator<Item = &'a Keyword>,
|
||||||
|
) -> Self {
|
||||||
|
let args = args
|
||||||
|
.into_iter()
|
||||||
|
.take_while(|arg| !matches!(arg.node, ExprKind::Starred { .. }))
|
||||||
|
.collect();
|
||||||
|
|
||||||
for arg in args {
|
let kwargs = keywords
|
||||||
match &arg.node {
|
.into_iter()
|
||||||
ExprKind::Starred { .. } => {
|
.filter_map(|keyword| {
|
||||||
break;
|
let node = &keyword.node;
|
||||||
}
|
node.arg.as_ref().map(|arg| (arg.as_ref(), &node.value))
|
||||||
_ => {
|
})
|
||||||
result.args.push(arg);
|
.collect();
|
||||||
}
|
|
||||||
}
|
SimpleCallArgs { args, kwargs }
|
||||||
}
|
}
|
||||||
|
|
||||||
for keyword in keywords {
|
/// Get the argument with the given name.
|
||||||
if let Some(arg) = &keyword.node.arg {
|
/// If the argument is not found by name, return
|
||||||
result.kwargs.insert(arg, &keyword.node.value);
|
/// `None`.
|
||||||
}
|
pub fn keyword_argument(&self, name: &str) -> Option<&'a Expr> {
|
||||||
}
|
self.kwargs.get(name).copied()
|
||||||
|
|
||||||
result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the argument with the given name or position.
|
/// Get the argument with the given name or position.
|
||||||
/// If the argument is not found with either name or position, return
|
/// If the argument is not found with either name or position, return
|
||||||
/// `None`.
|
/// `None`.
|
||||||
pub fn get_argument(&self, name: &'a str, position: Option<usize>) -> Option<&'a Expr> {
|
pub fn argument(&self, name: &str, position: usize) -> Option<&'a Expr> {
|
||||||
if let Some(kwarg) = self.kwargs.get(name) {
|
self.keyword_argument(name)
|
||||||
return Some(kwarg);
|
.or_else(|| self.args.get(position).copied())
|
||||||
}
|
|
||||||
if let Some(position) = position {
|
|
||||||
if position < self.args.len() {
|
|
||||||
return Some(self.args[position]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the number of positional and keyword arguments.
|
/// Return the number of positional and keyword arguments.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue