fix: new clippy warnings from rust 1.85.0 (#15203)

# Description
Mainly some cleanup of `map_or`.
This commit is contained in:
zc he 2025-02-27 21:11:47 +08:00 committed by GitHub
parent 12b8b4580c
commit 96af27fb4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 21 additions and 26 deletions

View File

@ -159,14 +159,11 @@ impl DirInfo {
fn add_file(mut self, f: impl Into<PathBuf>, params: &DirBuilder) -> Self { fn add_file(mut self, f: impl Into<PathBuf>, params: &DirBuilder) -> Self {
let f = f.into(); let f = f.into();
let include = params let include = params.exclude.as_ref().is_none_or(|x| !x.matches_path(&f));
.exclude
.as_ref()
.map_or(true, |x| !x.matches_path(&f));
if include { if include {
match FileInfo::new(f, params.deref, self.tag, self.long) { match FileInfo::new(f, params.deref, self.tag, self.long) {
Ok(file) => { Ok(file) => {
let inc = params.min.map_or(true, |s| file.size >= s); let inc = params.min.is_none_or(|s| file.size >= s);
if inc { if inc {
self.size += file.size; self.size += file.size;
self.blocks += file.blocks.unwrap_or(0); self.blocks += file.blocks.unwrap_or(0);

View File

@ -238,7 +238,7 @@ impl View for TryView {
} }
fn show_data(&mut self, i: usize) -> bool { fn show_data(&mut self, i: usize) -> bool {
self.table.as_mut().map_or(false, |v| v.show_data(i)) self.table.as_mut().is_some_and(|v| v.show_data(i))
} }
} }

View File

@ -457,7 +457,7 @@ where
.error(ErrorCode::LoneLeadingSurrogateInHexEscape)); .error(ErrorCode::LoneLeadingSurrogateInHexEscape));
} }
let n = (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) let n = ((((n1 - 0xD800) as u32) << 10) | (n2 - 0xDC00) as u32)
+ 0x1_0000; + 0x1_0000;
match char::from_u32(n) { match char::from_u32(n) {

View File

@ -142,25 +142,23 @@ where
fn serialize_f32(self, value: f32) -> Result<()> { fn serialize_f32(self, value: f32) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
fmt_f32_or_null(&mut self.writer, if value == -0f32 { 0f32 } else { value }) fmt_f32_or_null(&mut self.writer, if value == -0f32 { 0f32 } else { value })
.map_err(From::from)
} }
#[inline] #[inline]
fn serialize_f64(self, value: f64) -> Result<()> { fn serialize_f64(self, value: f64) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
fmt_f64_or_null(&mut self.writer, if value == -0f64 { 0f64 } else { value }) fmt_f64_or_null(&mut self.writer, if value == -0f64 { 0f64 } else { value })
.map_err(From::from)
} }
#[inline] #[inline]
fn serialize_char(self, value: char) -> Result<()> { fn serialize_char(self, value: char) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
escape_char(&mut self.writer, value).map_err(From::from) escape_char(&mut self.writer, value)
} }
#[inline] #[inline]
fn serialize_str(self, value: &str) -> Result<()> { fn serialize_str(self, value: &str) -> Result<()> {
quote_str(&mut self.writer, &mut self.formatter, value).map_err(From::from) quote_str(&mut self.writer, &mut self.formatter, value)
} }
#[inline] #[inline]
@ -496,7 +494,7 @@ where
#[inline] #[inline]
fn serialize_str(self, value: &str) -> Result<()> { fn serialize_str(self, value: &str) -> Result<()> {
escape_key(&mut self.ser.writer, value).map_err(From::from) escape_key(&mut self.ser.writer, value)
} }
type SerializeSeq = ser::Impossible<(), Error>; type SerializeSeq = ser::Impossible<(), Error>;
@ -853,7 +851,7 @@ pub fn escape_key<W>(wr: &mut W, value: &str) -> Result<()>
where where
W: io::Write, W: io::Write,
{ {
escape_bytes(wr, value.as_bytes()).map_err(From::from) escape_bytes(wr, value.as_bytes())
} }
#[inline] #[inline]

View File

@ -57,7 +57,7 @@ fn try_find_id_in_def(
) -> Option<(Id, Span)> { ) -> Option<(Id, Span)> {
let mut span = None; let mut span = None;
for arg in call.arguments.iter() { for arg in call.arguments.iter() {
if location.map_or(true, |pos| arg.span().contains(*pos)) { if location.is_none_or(|pos| arg.span().contains(*pos)) {
// String means this argument is the name // String means this argument is the name
if let Argument::Positional(expr) = arg { if let Argument::Positional(expr) = arg {
if let Expr::String(_) = &expr.expr { if let Expr::String(_) = &expr.expr {
@ -76,7 +76,7 @@ fn try_find_id_in_def(
let name = working_set.get_span_contents(span); let name = working_set.get_span_contents(span);
let decl_id = Id::Declaration(working_set.find_decl(name)?); let decl_id = Id::Declaration(working_set.find_decl(name)?);
id_ref id_ref
.map_or(true, |id_r| decl_id == *id_r) .is_none_or(|id_r| decl_id == *id_r)
.then_some((decl_id, span)) .then_some((decl_id, span))
} }
@ -96,7 +96,7 @@ fn try_find_id_in_mod(
location: Option<&usize>, location: Option<&usize>,
id_ref: Option<&Id>, id_ref: Option<&Id>,
) -> Option<(Id, Span)> { ) -> Option<(Id, Span)> {
let check_location = |span: &Span| location.map_or(true, |pos| span.contains(*pos)); let check_location = |span: &Span| location.is_none_or(|pos| span.contains(*pos));
call.arguments.first().and_then(|arg| { call.arguments.first().and_then(|arg| {
if !check_location(&arg.span()) { if !check_location(&arg.span()) {
@ -109,7 +109,7 @@ fn try_find_id_in_mod(
let found_id = Id::Module(module_id); let found_id = Id::Module(module_id);
let found_span = strip_quotes(arg.span(), working_set); let found_span = strip_quotes(arg.span(), working_set);
id_ref id_ref
.map_or(true, |id_r| found_id == *id_r) .is_none_or(|id_r| found_id == *id_r)
.then_some((found_id, found_span)) .then_some((found_id, found_span))
} }
_ => None, _ => None,
@ -152,7 +152,7 @@ fn try_find_id_in_use(
.or(working_set.find_variable(name).map(Id::Variable)), .or(working_set.find_variable(name).map(Id::Variable)),
_ => None, _ => None,
}; };
let check_location = |span: &Span| location.map_or(true, |pos| span.contains(*pos)); let check_location = |span: &Span| location.is_none_or(|pos| span.contains(*pos));
// Get module id if required // Get module id if required
let module_name = call.arguments.first()?; let module_name = call.arguments.first()?;
@ -232,10 +232,10 @@ fn try_find_id_in_overlay(
location: Option<&usize>, location: Option<&usize>,
id: Option<&Id>, id: Option<&Id>,
) -> Option<(Id, Span)> { ) -> Option<(Id, Span)> {
let check_location = |span: &Span| location.map_or(true, |pos| span.contains(*pos)); let check_location = |span: &Span| location.is_none_or(|pos| span.contains(*pos));
let module_from_overlay_name = |name: &str, span: Span| { let module_from_overlay_name = |name: &str, span: Span| {
let found_id = Id::Module(working_set.find_overlay(name.as_bytes())?.origin); let found_id = Id::Module(working_set.find_overlay(name.as_bytes())?.origin);
id.map_or(true, |id_r| found_id == *id_r) id.is_none_or(|id_r| found_id == *id_r)
.then_some((found_id, strip_quotes(span, working_set))) .then_some((found_id, strip_quotes(span, working_set)))
}; };
for arg in call.arguments.iter() { for arg in call.arguments.iter() {
@ -272,7 +272,7 @@ fn get_matched_module_id(
let path = std::path::PathBuf::from(name.as_ref()); let path = std::path::PathBuf::from(name.as_ref());
let stem = path.file_stem().and_then(|fs| fs.to_str()).unwrap_or(&name); let stem = path.file_stem().and_then(|fs| fs.to_str()).unwrap_or(&name);
let found_id = Id::Module(working_set.find_module(stem.as_bytes())?); let found_id = Id::Module(working_set.find_module(stem.as_bytes())?);
id.map_or(true, |id_r| found_id == *id_r) id.is_none_or(|id_r| found_id == *id_r)
.then_some((found_id, span)) .then_some((found_id, span))
} }

View File

@ -2817,7 +2817,7 @@ pub const DURATION_UNIT_GROUPS: &[UnitGroup] = &[
fn modf(x: f64) -> (f64, f64) { fn modf(x: f64) -> (f64, f64) {
let rv2: f64; let rv2: f64;
let mut u = x.to_bits(); let mut u = x.to_bits();
let e = ((u >> 52 & 0x7ff) as i32) - 0x3ff; let e = (((u >> 52) & 0x7ff) as i32) - 0x3ff;
/* no fractional part */ /* no fractional part */
if e >= 52 { if e >= 52 {

View File

@ -247,7 +247,7 @@ pub(crate) fn setup_config(
&config_file, &env_file, is_login_shell &config_file, &env_file, is_login_shell
); );
let create_scaffold = nu_path::nu_config_dir().map_or(false, |p| !p.exists()); let create_scaffold = nu_path::nu_config_dir().is_some_and(|p| !p.exists());
let result = catch_unwind(AssertUnwindSafe(|| { let result = catch_unwind(AssertUnwindSafe(|| {
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]

View File

@ -24,7 +24,7 @@ pub(crate) fn run_commands(
trace!("run_commands"); trace!("run_commands");
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
let create_scaffold = nu_path::nu_config_dir().map_or(false, |p| !p.exists()); let create_scaffold = nu_path::nu_config_dir().is_some_and(|p| !p.exists());
// if the --no-config-file(-n) option is NOT passed, load the plugin file, // if the --no-config-file(-n) option is NOT passed, load the plugin file,
// load the default env file or custom (depending on parsed_nu_cli_args.env_file), // load the default env file or custom (depending on parsed_nu_cli_args.env_file),
@ -54,7 +54,7 @@ pub(crate) fn run_commands(
perf!("read env.nu", start_time, use_color); perf!("read env.nu", start_time, use_color);
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
let create_scaffold = nu_path::nu_config_dir().map_or(false, |p| !p.exists()); let create_scaffold = nu_path::nu_config_dir().is_some_and(|p| !p.exists());
// If we have a config file parameter *OR* we have a login shell parameter, read the config file // If we have a config file parameter *OR* we have a login shell parameter, read the config file
if parsed_nu_cli_args.config_file.is_some() || parsed_nu_cli_args.login_shell.is_some() { if parsed_nu_cli_args.config_file.is_some() || parsed_nu_cli_args.login_shell.is_some() {
@ -122,7 +122,7 @@ pub(crate) fn run_file(
// if the --no-config-file(-n) flag is passed, do not load plugin, env, or config files // if the --no-config-file(-n) flag is passed, do not load plugin, env, or config files
if parsed_nu_cli_args.no_config_file.is_none() { if parsed_nu_cli_args.no_config_file.is_none() {
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
let create_scaffold = nu_path::nu_config_dir().map_or(false, |p| !p.exists()); let create_scaffold = nu_path::nu_config_dir().is_some_and(|p| !p.exists());
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file); read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file);
perf!("read plugins", start_time, use_color); perf!("read plugins", start_time, use_color);