-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clippy Pendantic #6720
base: main
Are you sure you want to change the base?
Clippy Pendantic #6720
Conversation
I don't have a windows machine and don't understand why the windows checks are failing. |
GNU testsuite comparison:
|
@@ -63,7 +63,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { | |||
} | |||
|
|||
let commands = match matches.get_many::<String>(options::COMMAND) { | |||
Some(v) => v.map(|s| s.as_str()).collect(), | |||
Some(v) => v.map(std::string::String::as_str).collect(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would prefer just
String::as_str
and have an import
repeating std::string isn't providing much value
@@ -99,7 +99,7 @@ impl<'a> Context<'a> { | |||
let current_dir = env::current_dir()?; | |||
let root_path = current_dir.join(root); | |||
let root_parent = if target.exists() && !root.to_str().unwrap().ends_with("/.") { | |||
root_path.parent().map(|p| p.to_path_buf()) | |||
root_path.parent().map(std::path::Path::to_path_buf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment about Path
root_path.parent().map(std::path::Path::to_path_buf) | |
root_path.parent().map(Path::to_path_buf) |
For the windows error, you can test using MS images: Here, for this error, the type is probably different on windows vs linux
|
@@ -112,7 +112,7 @@ impl FmtOptions { | |||
} | |||
(Some(w), None) => { | |||
// Only allow a goal of zero if the width is set to be zero | |||
let g = (w * DEFAULT_GOAL_TO_WIDTH_RATIO / 100).max(if w == 0 { 0 } else { 1 }); | |||
let g = (w * DEFAULT_GOAL_TO_WIDTH_RATIO / 100).max(usize::from(w != 0)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure to see the improvement here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should be rephrased anyway? Something like
let computed_goal = w * DEFAULT_GOAL_TO_WIDTH_RATIO / 100;
let g = if computed_goal > 0 {
computed_goal
} else if width == 0 {
0
} else {
1
}
This is probably not yet ideal, but it might make the intent clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting! Thanks! I like most of this, especially the format string improvements and the underscores in literals. I also think that it's important to consider that these are pedantic lints for a reason: they are more subjective than others. For instance, I dislike the map_or
lint in complex expressions, because I think it's easier to read the the map
and unwrap
parts as separate calls. I also agree with Sylvestre that the paths should be shortened for the map
calls.
Here are the commits I'd definitely accept:
- manual_assert
- uninlined_format_args
- unnecessary_join
- unreadable_literal
- inconsistent_struct_literal
- cloned_instead_of_copied
- flat_map_option
The more difficult cases that we should maybe split off into separate PRs for more discussion are:
- format_collect
- bool_to_int_with_if
- inefficient_to_string
- map_unwrap_or
- redundant_closure_for_method_calls
- used_underscore_binding (I agree with the lint, but maybe it can be fixed without
allow
s) - map_or_else
I think splitting this up makes it much easier to discuss. Hope that makes sense!
@@ -107,13 +107,15 @@ impl Filesystem { | |||
mount_info.mount_dir.clone() | |||
}; | |||
#[cfg(unix)] | |||
#[allow(clippy::used_underscore_binding)] | |||
let usage = FsUsage::new(statfs(_stat_path).ok()?); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there are reason to keep this as an underscore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I change it to not an underscore, then I would have to add #[allow(unused_variables)]
to the windows side of things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh. in this case I was worried about the possibility of other #[cfg(system)]
s existing, like one for browsers, or some other unknown system.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that's not the case, then why isn't it #[cfg(unix)]
and #[cfg(not(unix))]
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably should be yeah!
@@ -112,7 +112,7 @@ impl FmtOptions { | |||
} | |||
(Some(w), None) => { | |||
// Only allow a goal of zero if the width is set to be zero | |||
let g = (w * DEFAULT_GOAL_TO_WIDTH_RATIO / 100).max(if w == 0 { 0 } else { 1 }); | |||
let g = (w * DEFAULT_GOAL_TO_WIDTH_RATIO / 100).max(usize::from(w != 0)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should be rephrased anyway? Something like
let computed_goal = w * DEFAULT_GOAL_TO_WIDTH_RATIO / 100;
let g = if computed_goal > 0 {
computed_goal
} else if width == 0 {
0
} else {
1
}
This is probably not yet ideal, but it might make the intent clearer.
.opts | ||
.prefix | ||
.as_ref() | ||
.map_or(0, std::string::String::len)..] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can extract this to a variable:
let prefix_len = self.opts.prefix.as_ref().map_or(0, String::len);
I think that makes the intent and the formatting clearer and allows us to reuse the value.
@@ -149,7 +149,7 @@ mod tests { | |||
} | |||
|
|||
fn obsolete_result(src: &[&str]) -> Option<Result<Vec<String>, ParseError>> { | |||
Some(Ok(src.iter().map(|s| s.to_string()).collect())) | |||
Some(Ok(src.iter().map(|s| (*s).to_string()).collect())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What clippy lint triggered this? It seems to make it more complicted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inefficient_to_string
the compiler specializes the to_string in the changed case: https://rust-lang.github.io/rust-clippy/master/index.html#/to_string.
Do you suggest an easy way to split the commits up? I looked online and there were a few different suggestions including copy and pasting. Sorry for being unexperienced in git. |
make a backup and: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to get my comments out of "Pending" status.
@@ -107,13 +107,15 @@ impl Filesystem { | |||
mount_info.mount_dir.clone() | |||
}; | |||
#[cfg(unix)] | |||
#[allow(clippy::used_underscore_binding)] | |||
let usage = FsUsage::new(statfs(_stat_path).ok()?); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that's not the case, then why isn't it #[cfg(unix)]
and #[cfg(not(unix))]
.
@@ -149,7 +149,7 @@ mod tests { | |||
} | |||
|
|||
fn obsolete_result(src: &[&str]) -> Option<Result<Vec<String>, ParseError>> { | |||
Some(Ok(src.iter().map(|s| s.to_string()).collect())) | |||
Some(Ok(src.iter().map(|s| (*s).to_string()).collect())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inefficient_to_string
the compiler specializes the to_string in the changed case: https://rust-lang.github.io/rust-clippy/master/index.html#/to_string.
I ran
cargo clippy --workspace --all-targets --all-features -- --warn clippy::LINT
for all the lints listed in the below thread. I can't figure out what is different about running clippy through the filepre-commit-config.yaml
, but it gives different results. I fixed all the lints in the below listed thread except for clippy::match_same_arm, clippy::redundant_else, clippy::single_match_else, clippy::unnecessary_wraps, and clippy::unnested_or_patterns. They seemed either unlike they should be fixed or hard to fix.This works on #4949.