mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-25 05:53:51 +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"])
|
||||
{
|
||||
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 (int_value & WRITE_WORLD > 0) || (int_value & EXECUTE_GROUP > 0) {
|
||||
checker.diagnostics.push(Diagnostic::new(
|
||||
|
|
|
@ -25,7 +25,7 @@ impl Violation for HashlibInsecureHashFunction {
|
|||
const WEAK_HASHES: [&str; 4] = ["md4", "md5", "sha", "sha1"];
|
||||
|
||||
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!(
|
||||
&expr.node,
|
||||
ExprKind::Constant {
|
||||
|
@ -67,7 +67,7 @@ pub fn hashlib_insecure_hash_functions(
|
|||
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 WEAK_HASHES.contains(&hash_func_name.to_lowercase().as_str()) {
|
||||
checker.diagnostics.push(Diagnostic::new(
|
||||
|
|
|
@ -46,7 +46,7 @@ pub fn jinja2_autoescape_false(
|
|||
{
|
||||
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 {
|
||||
ExprKind::Constant {
|
||||
value: Constant::Bool(true),
|
||||
|
|
|
@ -33,7 +33,7 @@ pub fn logging_config_insecure_listen(
|
|||
{
|
||||
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(
|
||||
LoggingConfigInsecureListen,
|
||||
Range::from(func),
|
||||
|
|
|
@ -56,7 +56,7 @@ pub fn request_with_no_cert_validation(
|
|||
None
|
||||
}) {
|
||||
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 {
|
||||
value: Constant::Bool(false),
|
||||
..
|
||||
|
|
|
@ -44,7 +44,7 @@ pub fn request_without_timeout(
|
|||
})
|
||||
{
|
||||
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 {
|
||||
ExprKind::Constant {
|
||||
value: value @ Constant::None,
|
||||
|
|
|
@ -33,7 +33,7 @@ pub fn snmp_insecure_version(
|
|||
})
|
||||
{
|
||||
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 {
|
||||
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"])
|
||||
{
|
||||
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
|
||||
.ctx
|
||||
.resolve_call_path(loader_arg)
|
||||
|
|
|
@ -146,7 +146,7 @@ pub fn logging_call(checker: &mut Checker, func: &Expr, args: &[Expr], keywords:
|
|||
);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ impl Violation for FailWithoutMessage {
|
|||
pub fn fail_call(checker: &mut Checker, func: &Expr, args: &[Expr], keywords: &[Keyword]) {
|
||||
if is_pytest_fail(func, checker) {
|
||||
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 is_empty_or_null_string(msg) {
|
||||
|
|
|
@ -68,11 +68,11 @@ fn check_patch_call(
|
|||
new_arg_number: usize,
|
||||
) -> Option<Diagnostic> {
|
||||
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;
|
||||
}
|
||||
|
||||
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 {
|
||||
// Walk the lambda body.
|
||||
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 LoggingLevel::from_attribute(attr.as_str()).is_some() {
|
||||
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 {
|
||||
value: Constant::Str(value),
|
||||
..
|
||||
|
|
|
@ -1213,42 +1213,39 @@ pub struct SimpleCallArgs<'a> {
|
|||
}
|
||||
|
||||
impl<'a> SimpleCallArgs<'a> {
|
||||
pub fn new(args: &'a [Expr], keywords: &'a [Keyword]) -> Self {
|
||||
let mut result = SimpleCallArgs::default();
|
||||
pub fn new(
|
||||
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 {
|
||||
match &arg.node {
|
||||
ExprKind::Starred { .. } => {
|
||||
break;
|
||||
}
|
||||
_ => {
|
||||
result.args.push(arg);
|
||||
}
|
||||
}
|
||||
let kwargs = keywords
|
||||
.into_iter()
|
||||
.filter_map(|keyword| {
|
||||
let node = &keyword.node;
|
||||
node.arg.as_ref().map(|arg| (arg.as_ref(), &node.value))
|
||||
})
|
||||
.collect();
|
||||
|
||||
SimpleCallArgs { args, kwargs }
|
||||
}
|
||||
|
||||
for keyword in keywords {
|
||||
if let Some(arg) = &keyword.node.arg {
|
||||
result.kwargs.insert(arg, &keyword.node.value);
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
/// Get the argument with the given name.
|
||||
/// If the argument is not found by name, return
|
||||
/// `None`.
|
||||
pub fn keyword_argument(&self, name: &str) -> Option<&'a Expr> {
|
||||
self.kwargs.get(name).copied()
|
||||
}
|
||||
|
||||
/// Get the argument with the given name or position.
|
||||
/// If the argument is not found with either name or position, return
|
||||
/// `None`.
|
||||
pub fn get_argument(&self, name: &'a str, position: Option<usize>) -> Option<&'a Expr> {
|
||||
if let Some(kwarg) = self.kwargs.get(name) {
|
||||
return Some(kwarg);
|
||||
}
|
||||
if let Some(position) = position {
|
||||
if position < self.args.len() {
|
||||
return Some(self.args[position]);
|
||||
}
|
||||
}
|
||||
None
|
||||
pub fn argument(&self, name: &str, position: usize) -> Option<&'a Expr> {
|
||||
self.keyword_argument(name)
|
||||
.or_else(|| self.args.get(position).copied())
|
||||
}
|
||||
|
||||
/// Return the number of positional and keyword arguments.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue