#Issues with rust-analyzer when using clippy as check command.

172 messages · Page 1 of 1 (latest)

unborn blaze
#

Greetings.

I'm am using the rust-analyzer VSCode extension, and I'm trying to set it up to lint my code with clippy. I have the following configuration currently:

{
    "rust-analyzer.checkOnSave": true,
    "rust-analyzer.check.command": "clippy",
    "rust-analyzer.check.extraArgs": [
        "--",
        "-W",
        "clippy::all",
        "-W",
        "clippy::pedantic"
    ],
    "rust-analyzer.diagnostics.enable": true,
    "rust-analyzer.diagnostics.experimental.enable": false,
    "rust-analyzer.cargo.buildScripts.enable": true,
    "[rust]": {
        "editor.defaultFormatter": "rust-lang.rust-analyzer",
    }
}

I want to have the clippy::all and clippy::pedantic groups enabled, because I'm in the process of learning Rust and I think it might be helpful. The problem is that I'm having some weird issues with the extension.

Suppose we have the following code:

fn main() {
    let test = String::from("");
}

It will give me a warning:

warning: empty String is being created manually
  --> src/main.rs:13:16
   |
13 |     let test = String::from("");
   |                ^^^^^^^^^^^^^^^^ help: consider using: `String::new()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#manual_string_new
   = note: `-W clippy::manual-string-new` implied by `-W clippy::pedantic`
   = help: to override `-W clippy::pedantic` add `#[allow(clippy::manual_string_new)]`

So far so good. The problem is the following - after I go and refactor the code as suggested to use String::new() and then I save the file, the warning remains.

I don't understand why. I can open the command palette and run

rust-anaylzer: Restart server

Then the code gets analyzed again and the warning goes away as expected. But obviously that's not a solution.

Anyone have an idea how to fix this?

little prism
#

please dont do -W clippy::pedantic

#

you can remove all the flags

#

extra.Args

#

clippy::pedantic is sometimes really just annoying

unborn blaze
little prism
#

its not just annoying for you

#

its annoying for everyone

#

and half the lints in there are useless

unborn blaze
#

but is that the core of the issue I'm having?

#

where it's like the lint results get cached and stuck

#

unless i restart the language/extension server

#

I am trying to understand what is going on

little prism
unborn blaze
#

indeed, it seems to be that way

#

it's like it's analyzing an old version of the file after i save it

#

i've search about this issue online for probably an hour, couldn't find any useful info

little prism
#

when you save

unborn blaze
#

let me check

unborn blaze
little prism
#

on the left of rust-analyzer there should be a spinning loading wheel when you save

unborn blaze
#

I don't think so, but I think it's because my device is fast

little prism
unborn blaze
#

plently of times

#

i even created a brand new profile with no extensions or themes or anything

#

just for testing purposes

unborn blaze
#

so yeah, im completely lost right now

slim frost
#

i believe it may be rust-analyzer bug, it does exactly the same for me since like yesterday

unborn blaze
slim frost
#

haven't checked i assumed it's just zed that broke

unborn blaze
#

i'm using vscode, but i assume zed is just a fork and they use the same extension?

#

or was it a different editor

slim frost
#

it's a totally different editor

#

but they do have rust analyzer extension

unborn blaze
#

let me see if i can downgrade

#

if that's even possible

#

so im using
0.3.2786

slim frost
#

zed has an open issue about that

#

apparently rustup component add rust-analyzer fixed it for somebody

unborn blaze
#

can you link the issue please?

slim frost
unborn blaze
#

thank you

#

i think i might open an issue on the rust-analyzer repo, but let me see

#

still trying to install an older version

#

ok done, now im on 0.3.2777, gotta test

slim frost
#

i'm going to sleep now good luck

unborn blaze
#

(i think it fixes the issue btw)

little prism
unborn blaze
#

well, i am not sure what to say

#

which version are you using there?

#

0.3.2786?

little prism
#

but some version from rustup

#

all of my ones work

unborn blaze
#

well the issue seems to be with the most recent version only

#

which is from 1 day ago i think

little prism
#

i can update and check again

unborn blaze
#

opened an issue about it

#

for now i will stick with the previous version

little prism
#

this is vscode

#

try install normal r-a

#
rustup component add rust-analyzer
unborn blaze
#

i think i already have it

#
 ❯ rustup component list | grep rust-analyzer
rust-analyzer-x86_64-unknown-linux-gnu
unborn blaze
#

and if what you're saying is true, then why downgrading fixes the problem?

little prism
#
"rust-analyzer.server.path": "rust-analyzer"
#

add this to your settings.json

#

this will use system r-a instead of bundled

#

might fix issues

unborn blaze
unborn blaze
little prism
#

wait maybe they do also maintain the package

#

i just dont think its exactly a r-a issue

#

i think its vscode

#

i've had vscode issues before

unborn blaze
#

i think they do because when you try to open an issue, the template has some pre-filled spots for vscode and such

little prism
#

but no latest r-a works perfectly fine in nvim

unborn blaze
#

yeah def, i mean i have no idea what the issues is

#

so not trying to argue who's fault it is

#

hmmm

unborn blaze
#

i think

little prism
unborn blaze
#

maybe its because i don't have rust-analyzer itself installed properly

#

well for starters, i never explicitly installed it via rustup

#

❯ rust-analyzer
error: Unknown binary 'rust-analyzer' in official toolchain 'stable-x86_64-unknown-linux-gnu'.

Stack backtrace:
0: <unknown>
1: <unknown>
2: <unknown>
3: <unknown>
4: <unknown>
5: <unknown>
6: <unknown>
7: <unknown>
8: <unknown>
9: <unknown>
10: <unknown>
11: <unknown>
12: <unknown>
13: __libc_start_main
14: <unknown>

little prism
#

you dont have it

unborn blaze
#

yep yep

#

any idea what is this though?

❯ rustup component list | grep rust-analyzer
rust-analyzer-x86_64-unknown-linux-gnu

#

ohh

#

i get it

unborn blaze
#

no no i understand

#

i thought the list command shows list of installed modules

#

but it shows list of available modules

#

now after i installed it:

❯ rustup component list | grep rust-analyzer
rust-analyzer-x86_64-unknown-linux-gnu (installed)

little prism
unborn blaze
#

exactly

unborn blaze
#

i think it does, let me test the other project

unborn blaze
#

it definitely fixes it i believe

#

thank you for the tip 👍

#

tell me, what are the odds of me running into this rabbit hole exactly the day i wanted to try clippy for the first time 😅

#

i better update the issue with this info

little prism
#

but vscodes packaged r-a is eventually gonna break

#

i set it to use my system one on my pc as well

#

before i switched to nvim

unborn blaze
#

i do that for typescript and node too

#

to avoid certain issues

#

but i have near-zero famiiarity with the rust tooling so far so...

little prism
#

i just wouldnt suggest linting pedantic

#

it makes you need to spam #![expect] everywhere

unborn blaze
#

if it gets annoying i will remove it for sure

#

but i feel that it's good to have it on while learning

#

i mean, i haven't had much of a chance to even use it because i was dealing with the issue

unborn blaze
#

like - use pedantic but disable the rules that get annoying

little prism
#

lints get moved to pedantic for being opinionated or just throwing false positives

unborn blaze
little prism
#

can you run clippy -W pedantic in a terminal and paste the output here in a code block

unborn blaze
#

Here are a few:

warning: empty String is being created manually
  --> src/db.rs:64:28
   |
64 |         let mut contents = String::from("");
   |                            ^^^^^^^^^^^^^^^^ help: consider using: `String::new()`

warning: `format!(..)` appended to existing `String`
  --> src/db.rs:67:13
   |
67 |             contents.push_str(&format!("{key}:{value}\n"));
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: redundant closure
   --> src/db.rs:103:30
    |
103 |         self.rows.keys().map(|s| s.as_str()).collect()
    |                              ^^^^^^^^^^^^^^ help: replace the closure with the method itself: `std::string::String::as_str
#

also, there was another really useful one, let me see if i remember

little prism
#

but just dont set it as default in your lsp

#

if you want pedantic

#

you can add #![warn(clippy::pedantic)]

#

to the start of your code

unborn blaze
little prism
#

you should put these as
#[warn(xyz)]

unborn blaze
little prism
#

and also -W all is redundant

#

it already does

unborn blaze
#

because if they are in settings.json they will apply for every project globally

little prism
#

and for some projects

#

they dont care about pedantic and you will get a crazy number of warnings

unborn blaze
#

ok

  1. you can always create per-project config in .vscode/settings.json
  2. i see your point now
#

you could have said that, and yeah i agree

#

the thing is, i haven't started making any real projects yet, still on chapter 15 of the book

little prism
#

put it in your main rs

unborn blaze
#

yes, once i start working on more serious projects this is definitely the way to go 👍

little prism
#

so that anyone else using clippy outside the extension will also have -W pedantic

unborn blaze
#

agreed

#

actually

#

i might as well develop the good habit of doing that right away

#

but have to test how that works along with the extension

#

like if i dont pass extraArgs, will rust-analyzer pick the settings from main.rs

#

need to read/test

#

So i added #![warn(clippy::all, clippy::pedantic)] in my main.rs file and it seems the extension does pick it up.

I also tried disabling specific rules by adding this in Cargo.toml

[lints.clippy]
redundant_closure_for_method_calls = "allow"

though that doesn't seem to work unfortunately.

#

actually, running cargo clippy also seems to ignore that