Skip to content

Commit

Permalink
Use find_keyword helper function in more places (astral-sh#5993)
Browse files Browse the repository at this point in the history
## Summary

Use the `find_keyword` helper function instead of reimplementing it.

Follows on from astral-sh#5983 by doing a different search.

## Test Plan

`cargo test`
  • Loading branch information
tjkuson committed Jul 23, 2023
1 parent 97e31ca commit 74dc137
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use rustpython_parser::ast::{Expr, Keyword, Ranged};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::find_keyword;

use crate::checkers::ast::Checker;

Expand All @@ -28,12 +29,7 @@ pub(crate) fn logging_config_insecure_listen(
matches!(call_path.as_slice(), ["logging", "config", "listen"])
})
{
if keywords.iter().any(|keyword| {
keyword
.arg
.as_ref()
.map_or(false, |arg| arg.as_str() == "verify")
}) {
if find_keyword(keywords, "verify").is_some() {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rustpython_parser::ast::{self, Constant, Expr, Keyword, Ranged};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::find_keyword;

use crate::checkers::ast::Checker;

Expand Down Expand Up @@ -50,12 +51,7 @@ pub(crate) fn snmp_insecure_version(checker: &mut Checker, func: &Expr, keywords
matches!(call_path.as_slice(), ["pysnmp", "hlapi", "CommunityData"])
})
{
if let Some(keyword) = keywords.iter().find(|keyword| {
keyword
.arg
.as_ref()
.map_or(false, |arg| arg.as_str() == "mpModel")
}) {
if let Some(keyword) = find_keyword(keywords, "mpModel") {
if let Expr::Constant(ast::ExprConstant {
value: Constant::Int(value),
..
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rustpython_parser::ast::{self, Expr, Ranged, WithItem};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::find_keyword;

use crate::checkers::ast::Checker;

Expand Down Expand Up @@ -115,9 +116,7 @@ pub(crate) fn assert_raises_exception(checker: &mut Checker, items: &[WithItem])
.map_or(false, |call_path| {
matches!(call_path.as_slice(), ["pytest", "raises"])
})
&& !keywords
.iter()
.any(|keyword| keyword.arg.as_ref().map_or(false, |arg| arg == "match"))
&& find_keyword(keywords, "match").is_none()
{
AssertionKind::PytestRaises
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use rustpython_parser::ast::{Expr, Keyword, Ranged};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::find_keyword;

use crate::checkers::ast::Checker;

Expand Down Expand Up @@ -48,12 +49,7 @@ pub(crate) fn no_explicit_stacklevel(checker: &mut Checker, func: &Expr, keyword
return;
}

if keywords.iter().any(|keyword| {
keyword
.arg
.as_ref()
.map_or(false, |arg| arg.as_str() == "stacklevel")
}) {
if find_keyword(keywords, "stacklevel").is_some() {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Expr, Keyword, Ranged};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::is_const_none;
use ruff_python_ast::helpers::{find_keyword, is_const_none};
use ruff_python_semantic::SemanticModel;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -51,9 +51,7 @@ pub(crate) fn zip_without_explicit_strict(
if let Expr::Name(ast::ExprName { id, .. }) = func {
if id == "zip"
&& checker.semantic().is_builtin("zip")
&& !kwargs
.iter()
.any(|keyword| keyword.arg.as_ref().map_or(false, |name| name == "strict"))
&& find_keyword(kwargs, "strict").is_none()
&& !args
.iter()
.any(|arg| is_infinite_iterator(arg, checker.semantic()))
Expand Down
8 changes: 2 additions & 6 deletions crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ruff_diagnostics::{AlwaysAutofixableViolation, Violation};
use ruff_diagnostics::{Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::call_path::collect_call_path;
use ruff_python_ast::helpers::includes_arg_name;
use ruff_python_ast::helpers::{find_keyword, includes_arg_name};
use ruff_python_ast::identifier::Identifier;
use ruff_python_ast::visitor;
use ruff_python_ast::visitor::Visitor;
Expand Down Expand Up @@ -306,11 +306,7 @@ fn check_fixture_decorator(checker: &mut Checker, func_name: &str, decorator: &D
}

if checker.enabled(Rule::PytestExtraneousScopeFunction) {
let scope_keyword = keywords
.iter()
.find(|kw| kw.arg.as_ref().map_or(false, |arg| arg == "scope"));

if let Some(scope_keyword) = scope_keyword {
if let Some(scope_keyword) = find_keyword(keywords, "scope") {
if keyword_is_literal(scope_keyword, "function") {
let mut diagnostic =
Diagnostic::new(PytestExtraneousScopeFunction, scope_keyword.range());
Expand Down

0 comments on commit 74dc137

Please sign in to comment.