mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-17 17:10:34 +00:00
[ruff] Implement falsy-dict-get-fallback (RUF056) (#15160)
Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
68d2466832
commit
3c9021ffcb
51 changed files with 897 additions and 88 deletions
|
@ -3780,6 +3780,15 @@ pub enum ArgOrKeyword<'a> {
|
|||
Keyword(&'a Keyword),
|
||||
}
|
||||
|
||||
impl<'a> ArgOrKeyword<'a> {
|
||||
pub const fn value(&self) -> &'a Expr {
|
||||
match self {
|
||||
ArgOrKeyword::Arg(argument) => argument,
|
||||
ArgOrKeyword::Keyword(keyword) => &keyword.value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a Expr> for ArgOrKeyword<'a> {
|
||||
fn from(arg: &'a Expr) -> Self {
|
||||
Self::Arg(arg)
|
||||
|
@ -3828,15 +3837,24 @@ impl Arguments {
|
|||
.nth(position)
|
||||
}
|
||||
|
||||
/// Return the argument with the given name or at the given position, or `None` if no such
|
||||
/// argument exists. Used to retrieve arguments that can be provided _either_ as keyword or
|
||||
/// Return the value for the argument with the given name or at the given position, or `None` if no such
|
||||
/// argument exists. Used to retrieve argument values that can be provided _either_ as keyword or
|
||||
/// positional arguments.
|
||||
pub fn find_argument(&self, name: &str, position: usize) -> Option<&Expr> {
|
||||
pub fn find_argument_value(&self, name: &str, position: usize) -> Option<&Expr> {
|
||||
self.find_keyword(name)
|
||||
.map(|keyword| &keyword.value)
|
||||
.or_else(|| self.find_positional(position))
|
||||
}
|
||||
|
||||
/// Return the the argument with the given name or at the given position, or `None` if no such
|
||||
/// argument exists. Used to retrieve arguments that can be provided _either_ as keyword or
|
||||
/// positional arguments.
|
||||
pub fn find_argument(&self, name: &str, position: usize) -> Option<ArgOrKeyword> {
|
||||
self.find_keyword(name)
|
||||
.map(ArgOrKeyword::from)
|
||||
.or_else(|| self.find_positional(position).map(ArgOrKeyword::from))
|
||||
}
|
||||
|
||||
/// Return the positional and keyword arguments in the order of declaration.
|
||||
///
|
||||
/// Positional arguments are generally before keyword arguments, but star arguments are an
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue