Skip to content
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

[Merged by Bors] - Clippy improvements #4665

Closed
wants to merge 12 commits into from

Conversation

ManevilleF
Copy link
Contributor

@ManevilleF ManevilleF commented May 5, 2022

Objective

Follow up to my previous MR #3718 to add new clippy warnings to bevy:

There is one commit per clippy warning, and the matching flags are added to the CI execution.

To test the CI execution you may run cargo run -p ci -- clippy at the root.

I choose the add the flags in the ci tool crate to avoid having them in every lib.rs but I guess it could become an issue with suprise warnings coming up after a commit/push

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label May 5, 2022
@ManevilleF ManevilleF marked this pull request as ready for review May 5, 2022 12:50
@alice-i-cecile alice-i-cecile added C-Code-Quality A section of code that is hard to understand or change X-Controversial There is active debate or serious implications around merging this PR and removed S-Needs-Triage This issue needs to be labelled labels May 5, 2022
@@ -46,7 +46,7 @@ fn main() {
if what_to_run.contains(Check::CLIPPY) {
// See if clippy has any complaints.
// - Type complexity must be ignored because we use huge templates for queries
cmd!("cargo clippy --workspace --all-targets --all-features -- -A clippy::type_complexity -W clippy::doc_markdown -D warnings")
cmd!("cargo clippy --workspace --all-targets --all-features -- -A clippy::type_complexity -W clippy::doc_markdown -W clippy::option_if_let_else -W clippy::redundant_else -W clippy::match_same_arms -W clippy::semicolon_if_nothing_returned -W clippy::explicit_iter_loop -W clippy::map_flatten -D warnings")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use some string parsing to list out these lints in a cleaner fashion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried many different ways but xshell (even updated to 0.2) keeps printing the flags between double quotes, so I gave up

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure that xshell doesn't do argument splitting outside cmd! macro, so we need to keep the flags in a vec and expand like cmd!("cargo clippy {args...}")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that, and it still printed every flag between double quotes even though I'm sure I did exactly as the xshell doc does it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@infmagic2047 wether flags are stored in a Vec<&str>, a Vec<String>, or a [&str], const or in a let:

let flags = vec![
            "-A clippy::type_complexity",
            "-W clippy::doc_markdown",
            "-W clippy::option_if_let_else",
            "-W clippy::redundant_else",
            "-W clippy::match_same_arms",
            "-W clippy::semicolon_if_nothing_returned",
            "-W clippy::explicit_iter_loop",
            "-W clippy::map_flatten",
            "-D warnings",
        ];
cmd!("cargo clippy --workspace --all-targets --all-features -- {flags...}")

This is the output:

cargo clippy --workspace --all-targets --all-features -- "-A clippy::type_complexity" "-W clippy::doc_markdown" "-W clippy::option_if_let_else" "-W clippy::redundant_else" "-W clippy::match_same_arms" "-W clippy::semicolon_if_nothing_returned" "-W clippy::explicit_iter_loop" "-W clippy::map_flatten" "-D warnings"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-A clippy::type_complexity is actually two arguments -A and clippy::type_complexity, so writing them as one won't work. You can either write them as two elements, or use the concatenated form -Aclippy::type_complexity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks that seems to work, committing now

@alice-i-cecile
Copy link
Member

I like these lints; I think they add clarity and consistency without being tedious or frustrating.

The corresponding code changes are good: I think these are clear improvements in every case.

Added as a candidate to https://github.com/orgs/bevyengine/projects/4/views/1 to try to avoid bitrot.

@alice-i-cecile
Copy link
Member

Note: this gets the Controversial tag because it's a project-wide change to style.

Copy link
Member

@james7132 james7132 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I can agree with most of these changes, the use of map_or_else is a regression in terms of readability IMO. The use of two closures makes it visually hard to tell which branch is which.

@mockersf
Copy link
Member

mockersf commented May 6, 2022

map_or and map_or_else are bad for readability as they have the map closure as second parameter

@ManevilleF
Copy link
Contributor Author

ManevilleF commented May 6, 2022

I agree that in the case of large closures it's not great, I rwill ollback the changes and the restriction but should every change be rollbacked?

For example the changes in crates/bevy_ecs/src/schedule/run_criteria.rs

we go from:

if let Some(ref label) = self.label {
       std::slice::from_ref(label)
 } else {
       &[]
 }

to

self.label.as_ref().map_or(&[], std::slice::from_ref)

which to me is a clear improvement

@ManevilleF
Copy link
Contributor Author

I reverted the changes from the first commit (clippy::option_if_let_else) and removed the clippy warning in the CI.

Every added map_or and map_or_else was reverted, I just left a few instances of map where I thought it was better. I can revert those changes if necessary.

@mockersf @alice-i-cecile

@ManevilleF
Copy link
Contributor Author

Rebased on staging, fixed additional warnings

@IceSentry
Copy link
Contributor

IceSentry commented May 10, 2022

I'm not a huge fan of having redundant_else as a rule. I agree that it can sometimes be redundant, but in some situations I think it's clearer to show that each block are separate branches. I don't like that it's enforced in all situations. I haven't seen that many situations in this PR where the end result was necessarily worse, but I don't like enforcing that rule.

Copy link
Member

@james7132 james7132 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less code is better code. This doubles as a great cleanup as well.

@james7132
Copy link
Member

I'm not a huge fan of having redundant_else as a rule. I agree that it can sometimes be redundant, but in some situations I think it's clearer to show that each block are separate branches. I don't like that it's enforced in all situations. I haven't seen that many situations in this PR where the end result was necessarily worse, but I don't like enforcing that rule.

I personally like it because it just avoids the pressure to push code to the right, particularly if the code block is large.

@james7132 james7132 added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label May 24, 2022
@ManevilleF
Copy link
Contributor Author

Rebased on staging and fixed additional warnings. Maybe I should change the MR target to staging instead of main ? I'm not sure what the role of staging is

@IceSentry I don't think having a redundant else (meaning you return in the if) adds clarity, but maybe enforcing that rule is a bit much.

@james7132 Thanks for the review

@IceSentry
Copy link
Contributor

So apparently I was misunderstanding the redundant_else lint. I thought it would complain about code like this

fn foo(input: i32) -> String {
    if input == 42 {
        String::from("The answer!")
    } else {
        String::from("Wrong answerr")
    }
}

to turn it into this

fn foo(input: i32) -> String {
    if input == 42 {
        return String::from("The answer!");
    }
    String::from("Wrong")
}

just to avoid the last else, but that's not actually what it warns about. It will only warn if the else can be removed without adding or removing a return.

I still think it shouldn't be forced through a lint, but I'm not as opposed to it as I originally was.

@ManevilleF ManevilleF force-pushed the feat/clippy_restrictions branch 2 times, most recently from bae6e2c to 9cea556 Compare May 25, 2022 09:40
@cart cart force-pushed the feat/clippy_restrictions branch from 9cea556 to 4227dbf Compare May 31, 2022 01:37
@cart
Copy link
Member

cart commented May 31, 2022

Just resolved conflicts, rebased, and removed our manual allow(type_complexity) cases, now that they are allowed by default.

@cart
Copy link
Member

cart commented May 31, 2022

bors r+

bors bot pushed a commit that referenced this pull request May 31, 2022
# Objective

Follow up to my previous MR #3718 to add new clippy warnings to bevy:

- [x] [~~option_if_let_else~~](https://rust-lang.github.io/rust-clippy/master/#option_if_let_else) (reverted)
- [x] [redundant_else](https://rust-lang.github.io/rust-clippy/master/#redundant_else)
- [x] [match_same_arms](https://rust-lang.github.io/rust-clippy/master/#match_same_arms)
- [x] [semicolon_if_nothing_returned](https://rust-lang.github.io/rust-clippy/master/#semicolon_if_nothing_returned)
- [x] [explicit_iter_loop](https://rust-lang.github.io/rust-clippy/master/#explicit_iter_loop)
- [x] [map_flatten](https://rust-lang.github.io/rust-clippy/master/#map_flatten)

There is one commit per clippy warning, and the matching flags are added to the CI execution.

To test the CI execution you may run `cargo run -p ci -- clippy` at the root.

I choose the add the flags in the `ci` tool crate to avoid having them in every `lib.rs` but I guess it could become an issue with suprise warnings coming up after a commit/push


Co-authored-by: Carter Anderson <mcanders1@gmail.com>
@bors bors bot changed the title Clippy improvements [Merged by Bors] - Clippy improvements May 31, 2022
@bors bors bot closed this May 31, 2022
@ManevilleF ManevilleF deleted the feat/clippy_restrictions branch May 31, 2022 06:35
james7132 pushed a commit to james7132/bevy that referenced this pull request Jun 7, 2022
# Objective

Follow up to my previous MR bevyengine#3718 to add new clippy warnings to bevy:

- [x] [~~option_if_let_else~~](https://rust-lang.github.io/rust-clippy/master/#option_if_let_else) (reverted)
- [x] [redundant_else](https://rust-lang.github.io/rust-clippy/master/#redundant_else)
- [x] [match_same_arms](https://rust-lang.github.io/rust-clippy/master/#match_same_arms)
- [x] [semicolon_if_nothing_returned](https://rust-lang.github.io/rust-clippy/master/#semicolon_if_nothing_returned)
- [x] [explicit_iter_loop](https://rust-lang.github.io/rust-clippy/master/#explicit_iter_loop)
- [x] [map_flatten](https://rust-lang.github.io/rust-clippy/master/#map_flatten)

There is one commit per clippy warning, and the matching flags are added to the CI execution.

To test the CI execution you may run `cargo run -p ci -- clippy` at the root.

I choose the add the flags in the `ci` tool crate to avoid having them in every `lib.rs` but I guess it could become an issue with suprise warnings coming up after a commit/push


Co-authored-by: Carter Anderson <mcanders1@gmail.com>
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
# Objective

Follow up to my previous MR bevyengine#3718 to add new clippy warnings to bevy:

- [x] [~~option_if_let_else~~](https://rust-lang.github.io/rust-clippy/master/#option_if_let_else) (reverted)
- [x] [redundant_else](https://rust-lang.github.io/rust-clippy/master/#redundant_else)
- [x] [match_same_arms](https://rust-lang.github.io/rust-clippy/master/#match_same_arms)
- [x] [semicolon_if_nothing_returned](https://rust-lang.github.io/rust-clippy/master/#semicolon_if_nothing_returned)
- [x] [explicit_iter_loop](https://rust-lang.github.io/rust-clippy/master/#explicit_iter_loop)
- [x] [map_flatten](https://rust-lang.github.io/rust-clippy/master/#map_flatten)

There is one commit per clippy warning, and the matching flags are added to the CI execution.

To test the CI execution you may run `cargo run -p ci -- clippy` at the root.

I choose the add the flags in the `ci` tool crate to avoid having them in every `lib.rs` but I guess it could become an issue with suprise warnings coming up after a commit/push


Co-authored-by: Carter Anderson <mcanders1@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Code-Quality A section of code that is hard to understand or change S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Controversial There is active debate or serious implications around merging this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants