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

Fix clippy warnings #52

Merged
merged 1 commit into from
Dec 16, 2023
Merged

Fix clippy warnings #52

merged 1 commit into from
Dec 16, 2023

Conversation

nicmr
Copy link
Collaborator

@nicmr nicmr commented Dec 12, 2023

Description

Hi Ramilito,

nothing big this time, just used cargo clippy to check the code base for linter warnings.
Mostly small changes like superfluous dereferences or excessive .to_string().
The biggest change worth looking at would be the rewrite of the pattern matching in config.rs line 13 to avoid the unwrap().
We could also add a Github Action to automatically run cargo clippy on PRs to ensure code quality, what do you think?
For example https://github.com/giraffate/clippy-action, maintained by a member of the rust-lang clippy team.
We could add it in this PR or a separate one.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

No specific tests, just fixes linter warnings

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@nicmr
Copy link
Collaborator Author

nicmr commented Dec 13, 2023

Here are the fixed warnings:

❯ cargo clippy
    Checking kubesess v1.2.10 (~/kubesess)
warning: `to_string` applied to a type that implements `Display` in `format!` args
  --> src/commands.rs:18:48
   |
18 |             dirs::home_dir().unwrap().display().to_string()
   |                                                ^^^^^^^^^^^^ help: remove this
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#to_string_in_format_args
   = note: `#[warn(clippy::to_string_in_format_args)]` on by default

warning: `to_string` applied to a type that implements `Display` in `format!` args
  --> src/commands.rs:35:48
   |
35 |             dirs::home_dir().unwrap().display().to_string()
   |                                                ^^^^^^^^^^^^ help: remove this
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#to_string_in_format_args

warning: called `unwrap` on `ns` after checking its variant with `is_some`
  --> src/config.rs:14:9
   |
13 |     let ns = if ns.is_some() {
   |              --------------- help: try: `if let Some(..) = ns`
14 |         ns.unwrap().to_string()
   |         ^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap
   = note: `#[warn(clippy::unnecessary_unwrap)]` on by default

warning: redundant clone
  --> src/config.rs:25:26
   |
25 |             namespace: ns.to_string(),
   |                          ^^^^^^^^^^^^ help: remove this
   |
note: this value is dropped without further use
  --> src/config.rs:25:24
   |
25 |             namespace: ns.to_string(),
   |                        ^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
   = note: `#[warn(clippy::redundant_clone)]` on by default

warning: this expression creates a reference which is immediately dereferenced by the compiler
 --> src/config.rs:8:55
  |
8 |     let mut config: KubeConfig = serde_yaml::from_str(&strbuf).unwrap();
  |                                                       ^^^^^^^ help: change this to: `strbuf`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
  = note: `#[warn(clippy::needless_borrow)]` on by default

warning: useless use of `format!`
  --> src/config.rs:11:30
   |
11 |     config.current_context = format!("{}", ctx.name);
   |                              ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `ctx.name.to_string()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format
   = note: `#[warn(clippy::useless_format)]` on by default

warning: length comparison to zero
  --> src/config.rs:15:15
   |
15 |     } else if config.contexts.len() > 0 && !config.contexts[0].context.namespace.is_empty() {
   |               ^^^^^^^^^^^^^^^^^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!config.contexts.is_empty()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#len_zero
   = note: `#[warn(clippy::len_zero)]` on by default

warning: this expression creates a reference which is immediately dereferenced by the compiler
  --> src/config.rs:69:24
   |
69 |     let config = build(&ctx, namespace, &strbuf);
   |                        ^^^^ help: change this to: `ctx`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

warning: single-character string constant used as pattern
  --> src/config.rs:77:32
   |
77 |     for s in KUBECONFIG.rsplit(":") {
   |                                ^^^ help: try using a `char` instead: `':'`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern
   = note: `#[warn(clippy::single_char_pattern)]` on by default

warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> src/config.rs:113:51
    |
113 |     let config: KubeConfig = serde_yaml::from_str(&tmp.trim()).unwrap();
    |                                                   ^^^^^^^^^^^ help: change this to: `tmp.trim()`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

warning: unneeded late initialization
   --> src/config.rs:119:5
    |
119 |     let config;
    |     ^^^^^^^^^^^ created here
...
136 |     config = serde_yaml::from_str(&tmp.trim()).unwrap();
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ initialised here
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_late_init
    = note: `#[warn(clippy::needless_late_init)]` on by default
help: declare `config` here
    |
136 |     let config = serde_yaml::from_str(&tmp.trim()).unwrap();
    |     ~~~~~~~~~~

warning: unneeded late initialization
   --> src/config.rs:120:5
    |
120 |     let current;
    |     ^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_late_init
help: declare `current` here
    |
122 |     let current = if KUBESESSCONFIG.is_empty() {
    |     +++++++++++++
help: remove the assignments from the branches
    |
123 ~         KUBECONFIG.split(":").next().unwrap()
124 |     } else {
125 ~         KUBESESSCONFIG.as_str()
    |
help: add a semicolon after the `if` expression
    |
126 |     };
    |      +

warning: single-character string constant used as pattern
   --> src/config.rs:123:36
    |
123 |         current = KUBECONFIG.split(":").next().unwrap();
    |                                    ^^^ help: try using a `char` instead: `':'`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern

warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> src/config.rs:136:35
    |
136 |     config = serde_yaml::from_str(&tmp.trim()).unwrap();
    |                                   ^^^^^^^^^^^ help: change this to: `tmp.trim()`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

warning: `to_string` applied to a type that implements `Display` in `println!` args
  --> src/modes.rs:68:17
   |
68 |                 KUBECONFIG.to_string()
   |                 ^^^^^^^^^^^^^^^^^^^^^^ help: use this: `*KUBECONFIG`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#to_string_in_format_args

warning: unnecessary use of `to_string`
  --> src/modes.rs:68:27
   |
68 |                 KUBECONFIG.to_string()
   |                           ^^^^^^^^^^^^ help: remove this
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned
   = note: `#[warn(clippy::unnecessary_to_owned)]` on by default

warning: `to_string` applied to a type that implements `Display` in `println!` args
   --> src/modes.rs:110:9
    |
110 |         KUBECONFIG.to_string()
    |         ^^^^^^^^^^^^^^^^^^^^^^ help: use this: `*KUBECONFIG`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#to_string_in_format_args

warning: unnecessary use of `to_string`
   --> src/modes.rs:110:19
    |
110 |         KUBECONFIG.to_string()
    |                   ^^^^^^^^^^^^ help: remove this
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned

warning: single-character string constant used as pattern
  --> src/main.rs:18:46
   |
18 |                 for s in val.split_inclusive(":") {
   |                                              ^^^ help: try using a `char` instead: `':'`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern

warning: single-character string constant used as pattern
  --> src/main.rs:33:36
   |
33 |                 for s in val.split(":") {
   |                                    ^^^ help: try using a `char` instead: `':'`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern

warning: `kubesess` (bin "kubesess") generated 20 warnings (run `cargo clippy --fix --bin "kubesess"` to apply 18 suggestions)
    Finished dev [unoptimized + debuginfo] target(s) in 0.23s

let ns = match ns {
Some(namespace) => namespace.to_string(),
None => {
if !config.contexts.is_empty() && !config.contexts[0].context.namespace.is_empty() {
Copy link
Owner

Choose a reason for hiding this comment

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

Thank you! That function wasn't the prettiest one 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No worries, it was correct after all 😄

@@ -65,7 +65,7 @@ pub fn context(args: Cli) {
"{}/{}:{}",
&DEST.as_str(),
str::replace(&ctx, ":", "_"),
KUBECONFIG.to_string()
*KUBECONFIG
Copy link
Owner

Choose a reason for hiding this comment

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

I have no idea what the * here does but seems to work!

Copy link
Collaborator Author

@nicmr nicmr Dec 13, 2023

Choose a reason for hiding this comment

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

It's the dereferencing operator, usually used to retrieve concrete values from references, i.e. turn a &i64 into i64. It's available on all types implementing Deref .
lazy_static stores our lazy string behind a proxy struct pub struct Lazy<T: Sync>(Cell<Option<T>>, Once); that implements Deref to return the underlying value (T in the type signature, which is String in our case). So we are dereferencing the proxy struct to get the concrete String value.
Though I'd be lying if I said I really understand its implementation 😄
This thread tries to explain it: https://users.rust-lang.org/t/after-more-than-half-years-of-rust-in-production-i-still-dont-understand-how-lazy-static-works-what-am-i-doing-wrong/31870

@Ramilito
Copy link
Owner

Are you able to merge this yourself if it's approved? (curious about the permission settings)

@Ramilito
Copy link
Owner

Ramilito commented Dec 13, 2023

We could also add a Github Action to automatically run cargo clippy on PRs to ensure code quality, what do you think?
We could add it in this PR or a separate one.

I agree! I'll add it right away!

Edit: Added clippy-action but there seems to be a bug #84, I'l leave it for now

@nicmr
Copy link
Collaborator Author

nicmr commented Dec 13, 2023

Are you able to merge this yourself if it's approved? (curious about the permission settings)

I can't, Github shows me the following: Only those with [write access](https://docs.github.com/articles/what-are-the-different-access-permissions) to this repository can merge pull requests..

Edit: Added clippy-action but there seems to be a bug giraffate/clippy-action#84, I'l leave it for now

You're right 🤔 I'll give it a try in a test repo when I have the time :)

@Ramilito
Copy link
Owner

I can't, Github shows me the following: Only those with write access to this repository can merge pull requests..

Sent an invite to you as a collaborator but if you don't want that I can manage the merges!

@nicmr
Copy link
Collaborator Author

nicmr commented Dec 16, 2023

Thank you for your trust! I'm now indeed able to merge the approved PR :)

@nicmr nicmr merged commit d1ccfb3 into Ramilito:main Dec 16, 2023
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants