mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +00:00
Fix clippy::map_unwrap_or (pedantic)
https://rust-lang.github.io/rust-clippy/master/index.html#map_unwrap_or Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
parent
bc95690725
commit
3b33a431d6
8 changed files with 23 additions and 38 deletions
|
@ -121,10 +121,9 @@ pub fn match_call_path(
|
||||||
// `Match`).
|
// `Match`).
|
||||||
if num_segments == 0 {
|
if num_segments == 0 {
|
||||||
module.is_empty()
|
module.is_empty()
|
||||||
|| from_imports
|
|| from_imports.get(module).map_or(false, |imports| {
|
||||||
.get(module)
|
imports.contains(member) || imports.contains("*")
|
||||||
.map(|imports| imports.contains(member) || imports.contains("*"))
|
})
|
||||||
.unwrap_or(false)
|
|
||||||
} else {
|
} else {
|
||||||
let components: Vec<&str> = module.split('.').collect();
|
let components: Vec<&str> = module.split('.').collect();
|
||||||
|
|
||||||
|
@ -148,8 +147,7 @@ pub fn match_call_path(
|
||||||
let member = components[cut];
|
let member = components[cut];
|
||||||
if from_imports
|
if from_imports
|
||||||
.get(&module.as_str())
|
.get(&module.as_str())
|
||||||
.map(|imports| imports.contains(member))
|
.map_or(false, |imports| imports.contains(member))
|
||||||
.unwrap_or(false)
|
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -603,8 +603,7 @@ where
|
||||||
.node
|
.node
|
||||||
.asname
|
.asname
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|asname| asname == &alias.node.name)
|
.map_or(false, |asname| asname == &alias.node.name)
|
||||||
.unwrap_or(false)
|
|
||||||
{
|
{
|
||||||
Some((
|
Some((
|
||||||
self.scopes[*(self
|
self.scopes[*(self
|
||||||
|
@ -823,8 +822,7 @@ where
|
||||||
.node
|
.node
|
||||||
.asname
|
.asname
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|asname| asname == &alias.node.name)
|
.map_or(false, |asname| asname == &alias.node.name)
|
||||||
.unwrap_or(false)
|
|
||||||
{
|
{
|
||||||
Some((
|
Some((
|
||||||
self.scopes[*(self
|
self.scopes[*(self
|
||||||
|
@ -2341,8 +2339,7 @@ impl<'a> Checker<'a> {
|
||||||
.current_scope()
|
.current_scope()
|
||||||
.values
|
.values
|
||||||
.get(id)
|
.get(id)
|
||||||
.map(|binding| matches!(binding.kind, BindingKind::Global))
|
.map_or(false, |binding| matches!(binding.kind, BindingKind::Global))
|
||||||
.unwrap_or(false)
|
|
||||||
{
|
{
|
||||||
pep8_naming::plugins::non_lowercase_variable_in_function(self, expr, parent, id)
|
pep8_naming::plugins::non_lowercase_variable_in_function(self, expr, parent, id)
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,7 @@ fn is_abc_class(
|
||||||
.node
|
.node
|
||||||
.arg
|
.arg
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|a| a == "metaclass")
|
.map_or(false, |a| a == "metaclass")
|
||||||
.unwrap_or(false)
|
|
||||||
&& match_module_member(
|
&& match_module_member(
|
||||||
&keyword.node.value,
|
&keyword.node.value,
|
||||||
"abc",
|
"abc",
|
||||||
|
|
|
@ -21,7 +21,7 @@ pub fn categorize(
|
||||||
known_third_party: &BTreeSet<String>,
|
known_third_party: &BTreeSet<String>,
|
||||||
extra_standard_library: &BTreeSet<String>,
|
extra_standard_library: &BTreeSet<String>,
|
||||||
) -> ImportType {
|
) -> ImportType {
|
||||||
if level.map(|level| *level > 0).unwrap_or(false) {
|
if level.map_or(false, |level| *level > 0) {
|
||||||
ImportType::LocalFolder
|
ImportType::LocalFolder
|
||||||
} else if known_first_party.contains(module_base) {
|
} else if known_first_party.contains(module_base) {
|
||||||
ImportType::FirstParty
|
ImportType::FirstParty
|
||||||
|
|
|
@ -26,8 +26,7 @@ pub fn member_key<'a>(
|
||||||
} else if name
|
} else if name
|
||||||
.chars()
|
.chars()
|
||||||
.next()
|
.next()
|
||||||
.map(|char| char.is_uppercase())
|
.map_or(false, |char| char.is_uppercase())
|
||||||
.unwrap_or(false)
|
|
||||||
{
|
{
|
||||||
// Ex) `Class`
|
// Ex) `Class`
|
||||||
Prefix::Classes
|
Prefix::Classes
|
||||||
|
|
|
@ -11,13 +11,7 @@ use crate::python::string::{self};
|
||||||
/// N801
|
/// N801
|
||||||
pub fn invalid_class_name(class_def: &Stmt, name: &str) -> Option<Check> {
|
pub fn invalid_class_name(class_def: &Stmt, name: &str) -> Option<Check> {
|
||||||
let stripped = name.strip_prefix('_').unwrap_or(name);
|
let stripped = name.strip_prefix('_').unwrap_or(name);
|
||||||
if !stripped
|
if !stripped.chars().next().map_or(false, |c| c.is_uppercase()) || stripped.contains('_') {
|
||||||
.chars()
|
|
||||||
.next()
|
|
||||||
.map(|c| c.is_uppercase())
|
|
||||||
.unwrap_or(false)
|
|
||||||
|| stripped.contains('_')
|
|
||||||
{
|
|
||||||
return Some(Check::new(
|
return Some(Check::new(
|
||||||
CheckKind::InvalidClassName(name.to_string()),
|
CheckKind::InvalidClassName(name.to_string()),
|
||||||
Range::from_located(class_def),
|
Range::from_located(class_def),
|
||||||
|
|
|
@ -1224,8 +1224,7 @@ fn common_section(
|
||||||
if context
|
if context
|
||||||
.following_lines
|
.following_lines
|
||||||
.last()
|
.last()
|
||||||
.map(|line| !line.trim().is_empty())
|
.map_or(true, |line| !line.trim().is_empty())
|
||||||
.unwrap_or(true)
|
|
||||||
{
|
{
|
||||||
if context.is_last_section {
|
if context.is_last_section {
|
||||||
if checker.settings.enabled.contains(&CheckCode::D413) {
|
if checker.settings.enabled.contains(&CheckCode::D413) {
|
||||||
|
@ -1366,8 +1365,7 @@ fn args_section(checker: &mut Checker, definition: &Definition, context: &Sectio
|
||||||
if line
|
if line
|
||||||
.chars()
|
.chars()
|
||||||
.next()
|
.next()
|
||||||
.map(|char| char.is_whitespace())
|
.map_or(true, |char| char.is_whitespace())
|
||||||
.unwrap_or(true)
|
|
||||||
{
|
{
|
||||||
// This is a continuation of documentation for the last
|
// This is a continuation of documentation for the last
|
||||||
// parameter because it does start with whitespace.
|
// parameter because it does start with whitespace.
|
||||||
|
|
|
@ -83,9 +83,14 @@ impl Configuration {
|
||||||
.map_err(|e| anyhow!("Invalid dummy-variable-rgx value: {e}"))?,
|
.map_err(|e| anyhow!("Invalid dummy-variable-rgx value: {e}"))?,
|
||||||
None => DEFAULT_DUMMY_VARIABLE_RGX.clone(),
|
None => DEFAULT_DUMMY_VARIABLE_RGX.clone(),
|
||||||
},
|
},
|
||||||
src: options
|
src: options.src.map_or_else(
|
||||||
.src
|
|| {
|
||||||
.map(|src| {
|
vec![match project_root {
|
||||||
|
Some(project_root) => project_root.clone(),
|
||||||
|
None => path_dedot::CWD.clone(),
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
|src| {
|
||||||
src.iter()
|
src.iter()
|
||||||
.map(|path| {
|
.map(|path| {
|
||||||
let path = Path::new(path);
|
let path = Path::new(path);
|
||||||
|
@ -95,13 +100,8 @@ impl Configuration {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
})
|
},
|
||||||
.unwrap_or_else(|| {
|
),
|
||||||
vec![match project_root {
|
|
||||||
Some(project_root) => project_root.clone(),
|
|
||||||
None => path_dedot::CWD.clone(),
|
|
||||||
}]
|
|
||||||
}),
|
|
||||||
target_version: options.target_version.unwrap_or(PythonVersion::Py310),
|
target_version: options.target_version.unwrap_or(PythonVersion::Py310),
|
||||||
exclude: options
|
exclude: options
|
||||||
.exclude
|
.exclude
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue