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

Clippy fixes #3718

Closed
wants to merge 12 commits into from
Closed

Clippy fixes #3718

wants to merge 12 commits into from

Conversation

ManevilleF
Copy link
Contributor

@ManevilleF ManevilleF commented Jan 18, 2022

Objective

Fixes some nursery and pedantic clippy warnings like

  • clippy::missing_const_fn
  • clippy::option_if_let_else
  • clippy::use_self
  • clippy::redundant_else
  • clippy::match_same_arms
  • clippy::semicolon_if_returned
  • clippy::explicit_iter_loop
  • clippy::cloned_instead_of_copied
  • clippy::map_flatten

Solution

I applied many small changes accross all crates.

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Jan 18, 2022
@ManevilleF ManevilleF changed the title Clippy fixes Draft: Clippy fixes Jan 18, 2022
@ManevilleF ManevilleF changed the title Draft: Clippy fixes Clippy fixes Jan 18, 2022
Copy link
Contributor

@MinerSebas MinerSebas left a comment

Choose a reason for hiding this comment

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

There were several places where an fn was made const, that can't be actually called in a const context as the Type they are implemented for can only be constructed at runtime.
Please revert those functions or try to get a const-constructor.

Also please when change as many Files/Places as here, make 1. Commit for every clippy Lint. this makes it much easier to review each individual Lint.

crates/bevy_asset/src/loader.rs Outdated Show resolved Hide resolved
crates/bevy_asset/src/loader.rs Outdated Show resolved Hide resolved
crates/bevy_diagnostic/src/diagnostic.rs Outdated Show resolved Hide resolved
crates/bevy_diagnostic/src/diagnostic.rs Outdated Show resolved Hide resolved
crates/bevy_ecs/src/archetype.rs Outdated Show resolved Hide resolved
crates/bevy_core/src/time/timer.rs Outdated Show resolved Hide resolved
crates/bevy_core/src/time/timer.rs Outdated Show resolved Hide resolved
crates/bevy_core/src/time/timer.rs Outdated Show resolved Hide resolved
crates/bevy_core/src/time/timer.rs Outdated Show resolved Hide resolved
crates/bevy_core/src/time/timer.rs Outdated Show resolved Hide resolved
@ManevilleF
Copy link
Contributor Author

@MinerSebas const functions don't require a const context, there is no need for const constructors as it just allows the evaluation of the function at compile time.
Is there maybe a convention regarding const functions that I'm not aware of?

@mockersf
Copy link
Member

A const function can be evaluated at compile time, and will if all its parameters are const. In the cases raised by MinerSebas, there will never be a self that is const, so even if the functions are marked, they will never be evaluated at compile time.

Adding const here won't change anything for now, but it's an extra constraint on how Bevy may evolve as they may need to not be const later on and the person may be hesitant to remove it.

@mockersf mockersf added C-Code-Quality A section of code that is hard to understand or change and removed S-Needs-Triage This issue needs to be labelled labels Jan 18, 2022
@alice-i-cecile
Copy link
Member

Some of this work was duplicated in #2934. I think I prefer the clarity and freshness of this PR, if we're picking one to merge.

@ManevilleF
Copy link
Contributor Author

ManevilleF commented Jan 19, 2022

Thanks @mockersf (Merci François !) I'll apply the suggestions of @MinerSebas and remove the extra const I added

@alice-i-cecile I added more clippy fixes like clippy::semicolon_if_nothing_is_returned and clippy::explicit_iter_loop which are in the similar PR.

Should I add to the CI config or the lib.rs files the now fixed clippy warnings to avoid them in the future?

@@ -570,6 +570,7 @@ impl World {

/// Returns an iterator of entities that had components of type `T` removed
/// since the last call to [`World::clear_trackers`].
// TODO: return `std::iter::Copied` instead
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 think we should return a std::iter::Copied here but I don't know if it would be a breaking change or not

@@ -579,6 +580,7 @@ impl World {

/// Returns an iterator of entities that had components with the given `component_id` removed
/// since the last call to [`World::clear_trackers`].
// TODO: return `std::iter::Copied` instead
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 think we should return a std::iter::Copied here but I don't know if it would be a breaking change or not

@alice-i-cecile
Copy link
Member

Yeah, if we're going to fix pedantic / nursery lints we should toggle them on in CI so they stay fixed.

@mockersf
Copy link
Member

mockersf commented Jan 19, 2022

Yeah, if we're going to fix pedantic / nursery lints we should toggle them on in CI so they stay fixed.

To be transparent, this is exactly why I think most of those lints should not be fixed. Adding more things to check will slow down development. Some add enough values to be worth it, but that's not the case of most of the pedantic lints.

Also, the pedantic lints are often quite... pedantic and a matter of taste. Some of them are fine, I disagree with a few, and some should be done only sometimes - doing them by a lint remove sense from the code.

@ManevilleF
Copy link
Contributor Author

@mockersf I agree, many pedantic lints are not worth it.
I only fixed a few that I think are good practice or useful and ignored the others.

@alice-i-cecile
Copy link
Member

@ManevilleF do you want to rebase this? If so we can merge it in ASAP after rebasing; otherwise I think we should just close it out to reduce merge conflicts.

@ManevilleF
Copy link
Contributor Author

As discussed I will close this and open it again from the main branch with the warnings enabled on the CI.

@mockersf @cart I think the current list of warnings is a good start:

    clippy::option_if_let_else
    clippy::use_self
    clippy::redundant_else
    clippy::match_same_arms
    clippy::semicolon_if_returned
    clippy::explicit_iter_loop
    clippy::cloned_instead_of_copied
    clippy::map_flatten

Do you have more warnings you wish to add or some to remove?

@ManevilleF ManevilleF closed this Apr 22, 2022
@alice-i-cecile
Copy link
Member

alice-i-cecile commented Apr 22, 2022

List of lints, for easy reference.

@alice-i-cecile
Copy link
Member

Personal opinions:

  1. option_if_let_else: genuinely clearer.
  2. use_self: actively dislike this. See use Self keyword #4152.
  3. redundant_else: seems nice
  4. match_same_arms: nice clarity win
  5. semicolon_if_nothing_returned: presuming this is what you meant? Seems like a minor improvement.
  6. explicit_iter_loop: no strong feelings
  7. cloned_instead_of_copied: eh sure why not
  8. map_flatten: I like it! Much clearer.

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>
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants