Show unused code from multi-crate Rust projects

Overview

Warnalyzer

Remove unused code from multi-crate Rust projects.

The dead_code lint family of rustc is limited to one crate only and thus can't tell whether some public API is used inside a multi-crate project or not.

This tool, warnalyzer, provides unused code detection functionality for such multi-crate projects.

Usage

  • Navigate to the project you want to analyze and run RUSTFLAGS="-Z save-analysis" cargo +nightly check.
  • This command puts save analysis data into a path like target/debug/deps/save-analysis/cratename-longhash.json.
  • Then, from the warnalyzer repo, do cargo run <path-to-json>
  • It will list any items that it thinks weren't used.
  • Make sure that you chose the json of the leaf crate. Using any other json file won't give you the full list of unused code.

Requirements

Nightly rust is required, as save-analysis is unstable. Despite the name, warnalyzer does not use rust-analyzer (yet).

Known bugs

It's still early on. There are a couple of bugs of the tool.

false-positives

These are the false positives known to me:

Other bugs

  • Enum variants are not recognized (worked around in the code but it would be cool to have the rustc bug fixed)
  • Uses in enum struct variants are not recognized as such (upstream bug)
  • For loops: The expr part of for pat in expr {} is not seen as "use" of whatever the expression contains.
  • No recursion like the dead_code lint of rustc, so if something only gets used by unused code, it doesn't get reported while it should.
  • Multi-leaf crate trees are not supported (yet). Some projects may have multiple binaries they produce. Almost every project has tests. Some functions may only be used by some of those roots.

Trophy case

License

This crate is distributed under the terms of both the MIT license and the Apache License (Version 2.0), at your option.

See LICENSE for details.

License of your contributions

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Comments
  • Release a new version?

    Release a new version?

    The released version 0.1.0 does not compile now:

    error[E0432]: unresolved imports `syn::visit`, `syn::visit`
      --> /home/efanzh/.cargo/registry/src/github.com-1ecc6299db9ec823/warnalyzer-0.1.0/src/mute.rs:88:11
       |
    88 |     use syn::visit::{visit_item, self};
       |              ^^^^^               ^^^^ no `visit` in the root
       |              |
       |              could not find `visit` in `syn`
    
    error[E0433]: failed to resolve: could not find `visit` in `syn`
       --> /home/efanzh/.cargo/registry/src/github.com-1ecc6299db9ec823/warnalyzer-0.1.0/src/mute.rs:105:22
        |
    105 |     impl<'ast, 'a> syn::visit::Visit<'ast> for Visitor<'a> {
        |                         ^^^^^ could not find `visit` in `syn`
    
    error: aborting due to 2 previous errors
    
    Some errors have detailed explanations: E0432, E0433.
    For more information about an error, try `rustc --explain E0432`.
    error: failed to compile `warnalyzer v0.1.0`, intermediate artifacts can be found at `/tmp/cargo-installCYhgjx`
    
    Caused by:
      could not compile `warnalyzer`
    
    To learn more, run the command again with --verbose.
    
    opened by EFanZh 2
  • Trait implementation methods not considered as used

    Trait implementation methods not considered as used

    trait Abc {
        fn a();
        fn b();
    }
    
    struct Bcd;
    
    impl Abc for Bcd {
        fn a() {}
        fn b() {} // considered unused even though it is required
    }
    
    fn main() {
        Bcd::a();
    }
    
    opened by bjorn3 2
  • Migrate to SCIP

    Migrate to SCIP

    save-analysis is going away. For the short to mid term future, we can pin nightlies but with increased MSRV requirements, this will cause issues.

    Some more info on it in this reddit thread.

    opened by est31 0
  • Skip private items

    Skip private items

    Private items are already warned about by rustc's dead_code lint, and because of the bugs around macros, almost all warnings about private code are false positives.

    opened by jyn514 1
  • Regard entry points as used

    Regard entry points as used

    Right now, warnalyzer regards the main function of a program as unused. It would be great if it could somehow recognize that something is an entry point and record it as used.

    opened by est31 0
  • Very buggy on rustc itself

    Very buggy on rustc itself

    Probably most of these are because -Z save-analysis is buggy, but I don't know warnalyzer well enough to tell myself. Also, I want you to know this tool has been super useful :heart: and I'm going to make a PR removing dead code shortly; it just happens to also be very buggy at the same time :laughing: I'm going to put them all in one issue to avoid spamming your bug tracker, but happy to separate them out if you think it's useful.

    You should be able to reproduce with RUSTFLAGS_BOOTSTRAP="-Z save-analysis" x.py check on https://github.com/rust-lang/rust/commit/2b5c78488e1a9bbc27ecf96d351e880a808077a0.

    • compiler/rustc/src/main.rs:1:4: unused Function 'main' - clearly wrong, this is the entrypoint of the compiler
    • compiler/rustc/src/main.rs:1:1: unused Mod '' - I think this is trying to warn about the crate root? there's at least one bug here
    • compiler/rustc_ast/src/ast_like.rs:114:8: unused Method 'visit' - this is a trait function and can't be used. There are some implementations of the trait which are used.
    • compiler/rustc_apfloat/src/ieee.rs:166:5: unused Type 'Single' - this is defined in a macro, which you mentioned in the readme is buggy.
    • compiler/rustc_apfloat/src/lib.rs:64:10: unused Type 'Copy' - this is a derived trait, not a type.
    • compiler/rustc_apfloat/src/lib.rs:576:14: unused Type '$t' - this should show what $t is, since the macro could be called multiple times.
    • compiler/rustc_ast/src/ast.rs:163:20: unused Field '0' - this is for GenericArgs::AngleBracketed(AngleBracketedArgs). Not sure what's going on here, these are used quite a lot.
    • compiler/rustc_codegen_ssa/src/common.rs:109:5: unused Mod 'temp_stable_hash_impls' - the impl in this mod is used, even though it has no other items.
    • compiler/rustc_hir/src/hir.rs:3040:12: unused Method 'hir_id' - this is used outside of a macro, in compiler/rustc_middle/src/hir/map/mod.rs:304:28.
    • compiler/rustc_hir/src/definitions.rs:94:12: unused Method 'enumerated_keys_and_path_hashes' - this is used in compiler/rustc_metadata/src/rmeta/encoder.rs:462:62
    • compiler/rustc_middle/src/mir/mod.rs:2472:12: unused Method 'projections_and_spans' - this is used in compiler/rustc_middle/src/mir/mod.rs:2472:12: unused Method 'projections_and_spans':403

    There are a bunch more macro issues, but I didn't report them all since you say in the readme they're imperfectly supported.

    opened by jyn514 3
  • Adopt rust-analyzer

    Adopt rust-analyzer

    https://github.com/rust-analyzer/rust-analyzer

    API docs: https://rust-analyzer.github.io/rust-analyzer/ra_ide_api/index.html

    Not sure whether it's up to the task yet.

    opened by est31 3
Owner
FLOSS software. Past contributions include @minetest, @rust-lang, F-Droid.
null
belt is a command line app that can show your time from a list of selected time zones

A CLI app to show your time from a list of selected time zones, and a rust lib to parse dates in string formats that are commonly used.

Rollie Ma 23 Nov 4, 2022
A simple program to show a histogram on the terminal.

A simple program to show a histogram on the terminal.

依云 4 Aug 10, 2021
Show active TCP connections on a TUI world map.

Maperick Show active TCP connections on a TUI world map. Still WIP, but it's gonna be good. Setup git clone [email protected]:schlunsen/maperick.git cd m

Rasmus Schlünsen 5 Jul 8, 2022
👑 Show in-organization ranking of GitHub activities such as review count.

gh-ranking Show in-organization ranking of GitHub activities such as review count. Installation gh extension install yukukotani/gh-ranking Usage USAG

Yuku Kotani 2 Jul 28, 2022
🚩 Show sensitive command summary when open a new terminal

?? Show sensitive command summary when open a new terminal ?? Clear sensitive commands from shell history ?? Stash your history command before present

Rusty Ferris Club 149 Nov 16, 2022
A tiny crate to make it easy to share and apply Git hooks for Rust projects

Shareable git hooks for Rust project. Sloughi is a friend of Husky from North Africa! :algeria:

Walid ZIOUCHE 24 Oct 6, 2022
Mercy is a public Rust crate created to assist with building cybersecurity frameworks, assessment tools, and numerous other projects

Mercy ?? Documentation Mercy is a public Rust crate created to assist with building cybersecurity frameworks, assessment tools, and numerous other pro

Umiko Security 2 Nov 3, 2022
Mercy is a public Rust crate created to assist with building cybersecurity frameworks, assessment tools, and numerous other projects

Mercy ?? Documentation Mercy is a public Rust crate created to assist with building cybersecurity frameworks, assessment tools, and numerous other pro

CyberSuki 2 Nov 27, 2022
a cargo subcommand for counting lines of code in Rust projects

cargo-count Linux: A cargo subcommand for displaying line counts of source code in projects, including a niave unsafe counter for Rust source files. T

Kevin K. 124 Nov 23, 2022
Multi-Architecture Code Emission Library

macel Multi-Architecture Code Emission Library (macel) is a library which implements a low-level intermediate representation meant to expose on machin

merry 11 Oct 27, 2021
Curated list of awesome projects and resources related to Rust and computer security

Awesome Rust Security Curated list of awesome projects and resources related to Rust and computer security Table of Contents Tools Web and Cloud Secur

Alan 116 Nov 26, 2022
A blazing fast command line license generator for your open source projects written in Rust🚀

Overview This is a blazing fast ⚡ , command line license generator for your open source projects written in Rust. I know that GitHub

Shoubhit Dash 38 Nov 20, 2022
Just a collection of tiny Rust projects I've did. None warrant a whole repo rn

Daily Rust I try to write some tiny programs daily to gradually improve my Rust skills! Current Program Descriptions first_prog.rs: My first program a

null 3 Nov 5, 2022
Rust crate to enable ANSI escape code support on Windows.

enable-ansi-support: Enable ANSI escape code support on Windows 10 About This crate provides one function, enable_ansi_support, which allows ANSI esca

Rain 9 Nov 18, 2022
CLI program for building generative projects

NFT image generator This is the project to help artists to generate NFT collection, which can be generated from layers of PNG images. Usage Get archiv

null 10 Apr 27, 2022
A few demos showing how to estimate projects using Monte Carlo simulations.

Agile Monte Carlo Simulations Demos This is the repository which accompanies the blog post "How to replace estimations and guesses with a Monte Carlo

Lucas F. da Costa 14 Jun 18, 2022
A simple CLI to build VEXCode V5 Pro projects and download them to the V5 brain.

vexer A simple CLI to build VEXCode V5 Pro projects and download them to the V5 brain. (WIP) This currently is only tested on and only works on window

null 2 May 16, 2022
A Command-line tool to create, manage and deploy your python projects

PPM A Command-line tool to create, manage and deploy your python projects Table of Contents PPM Main Features Create a Project project.ini file Projec

FUSEN 6 Aug 30, 2022
CLI tool to quickly create React + Typescript + Tailwind projects

QUICK INIT CLI Tool to quickly create React projects with Tailwind automatically configured for use. Typescript is selected as default. If JS is to be

Oscar 5 Nov 3, 2022