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

Ignore some warnings from rustc #3765

Closed
NickeZ opened this issue Feb 26, 2017 · 17 comments
Closed

Ignore some warnings from rustc #3765

NickeZ opened this issue Feb 26, 2017 · 17 comments

Comments

@NickeZ
Copy link

NickeZ commented Feb 26, 2017

I would like to be able to ignore "dead_code" when I build with cargo with neomake inside neovim. I don't want to add exceptions to the sources because I want the warnings when I compile manually in the shell. I also don't think it is possible to set env vars in neomake so I can't use RUSTFLAGS. This is clearly a productivity issue since I'm distracted by irrelevant warnings when I'm in the exploration phase of a project.

@jbendig
Copy link
Contributor

jbendig commented Feb 27, 2017

I'm not familiar with neomake but if you're only building one target, you can try using cargo rustc instead of cargo build. For example, if you're working on a lib: cargo rustc --lib -- -A dead_code.

@NickeZ
Copy link
Author

NickeZ commented Feb 27, 2017

I get no library targets found if I run that command. Btw, it would be great if the arguments work no matter which kind of project I build..

I obviously can't use rustc alone on the file because all the imports fail.

@NickeZ
Copy link
Author

NickeZ commented Feb 27, 2017

Either I need the possibility to inform rustc of all the dependencies so that it doesn't fail on imports. Or I need the possibility to tell cargo to use rustc with some specific flags, without resorting to environment variables...

@jbendig
Copy link
Contributor

jbendig commented Feb 27, 2017

Are you building a binary instead of a library? If you have a src/main.rs and no [[bin]] sections in your Cargo.toml file, then try this instead: cargo rustc --bin <project-name> -- -A dead_code where <project_name> is the name of your project.

Passing arbitrary flags to rustc was discussed in #60 but was closed in favor of per-project profiles. Unfortunately, that won't do what you want even if an appropriate field was available. I think the above command will work for your use case though.

@NickeZ
Copy link
Author

NickeZ commented Feb 27, 2017

So you mean that I need to parse the toml file to figure out which flags to pass to cargo? I really want to be able to configure this once in my editor and still have it work with any rust file. If I didn't think that this is a usability problem I wouldn't have posted an issue about it.

@jbendig
Copy link
Contributor

jbendig commented Feb 27, 2017

So you mean that I need to parse the toml file to figure out which flags to pass to cargo?

For a general solution that doesn't use environment variables, yes, as far as I know. But, if you're working on a project with one binary, then it's not something you have to change as your project grows and you add more source files.

Where cargo build can build any number of targets (binaries, examples, etc.) in one command, cargo rustc needs to know the specific one. See cargo rustc --help for how to specify which target. If you have only one binary in your project, you can just run cargo rustc -- -A dead_code without specifying a target explicitly.

Maybe consider this as a temporary solution?

I really want to be able to configure this once in my editor and still have it work with any rust file. If I didn't think that this is a usability problem I wouldn't have posted an issue about it.

Sure, seems reasonable. I've had problems when environment variables were not available with build systems for other programming languages. An alternative might be nice but I'm not familiar with the decisions that lead to not allowing passing flags to rustc directly. Maybe @alexcrichton can chime in?

@NickeZ
Copy link
Author

NickeZ commented Feb 27, 2017

As a temporary solution I will set RUSTFLAGS in my bashrc. It works with any source code, I just have to remember to unset it when I want to see dead_code and unused_imports and so on in the future again...

@NickeZ
Copy link
Author

NickeZ commented Mar 1, 2017

One issue with using RUSTFLAGS is that it recompiles all the modules if I set/unset RUSTFLAGS...

Is this an issue in rustc? should it recompile stuff if I change warnings that are emitted?

@alexcrichton
Copy link
Member

@NickeZ that's intended behavior currentl, Cargo treats RUSTFLAGS opaquely and will recompile code when it changes (because that affects how code is compiled)

@NickeZ
Copy link
Author

NickeZ commented Mar 2, 2017

@alexcrichton So there is no way to filter the warnings from rustc without affecting how the code is compiled? This blew my plan that I was going to compile with fewer warnings in vim and all warnings in the terminal.

I'm sure the rust language server will be awesome when it is finished. But right now it would have been great if I could have filtered the warnings when neomake is compiling in the background so that I can focus on the real issues when I'm developing.

@alexcrichton
Copy link
Member

AFAIK, yeah, there's no great way to do that right now short of writing your own rustc shim.

@NickeZ
Copy link
Author

NickeZ commented Mar 3, 2017

Hmm you mean that I should have my own rustc shell script that calls rustc with/withut -A dead_code depending on some other environment variable?

I guess that cargo will recompile everything if I switch between my shim and the real rustc?

@alexcrichton
Copy link
Member

Yeah if you switch compilers Cargo will recompile, but you could have your own script read an env var and that'd subvert Cargo's caching mechanism.

@NickeZ
Copy link
Author

NickeZ commented Mar 3, 2017

Great, I'll try something like this:

#!/bin/sh
if [ -z "${NEOMAKE_CARGO}" ] ; then
    "$HOME/.cargo/bin/rustc" "${@}"
else
    "$HOME/.cargo/bin/rustc" -A dead_code "${@}"
fi

edit: now I just have to implement environment variables in neomake...

@alexcrichton
Copy link
Member

Yeah that'd do it!

@NickeZ
Copy link
Author

NickeZ commented Mar 6, 2017

@alexcrichton I can't get it to work. How do I check which rustc is used by cargo? How do I override that?

$ cat ~/.local/bin/rustc
#!/bin/sh
if [ -z "${NEOMAKE_CARGO}" ] ; then
        echo "rustc regular" >&2
        "$HOME/.cargo/bin/rustc" "${@}"
else
        echo "rustc without dead_code" >&2
        "$HOME/.cargo/bin/rustc" -A dead_code "${@}"
fi
$ which rustc
/home/niklas/.local/bin/rustc
$ rustc --help
rustc without dead_code
Usage: rustc [OPTIONS] INPUT
...
$ echo $NEOMAKE_CARGO
1
$ cargo build
   Compiling rups v0.1.0 (file:///home/niklas/git/procServ-ng)
warning: field is never used: `noinfo`
  --> src/telnet_server.rs:41:5
   |
41 |     noinfo: bool,
   |     ^^^^^^^^^^^^
   |
   = note: #[warn(dead_code)] on by default

@NickeZ
Copy link
Author

NickeZ commented Mar 6, 2017

OK it's working I had to set RUSTC=~/.local/bin/rustc. But overall it is a pretty bad experience...

@NickeZ NickeZ closed this as completed Mar 6, 2017
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

No branches or pull requests

3 participants