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
A simple cli to clone projects and fetch all projects in a GitHub org..

stupid-git A simple cli to clone projects and update all projects. get all repository from GitHub clone all pull all with git stash Usage create sgit.

Fengda Huang 5 Sep 15, 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 27, 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 very basic show-case of rust on the esp32 in 2022

Readme This example code does the following: Set up a WiFi connection on the ESP32-C3 Spawn a thread using std::thread in which we listen for incoming

Mattia 13 Jan 19, 2023
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 3 Dec 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 161 Dec 26, 2022
🎨✨ Show off your soothing color palette

?? Show off your soothing color palette ✨ Palettes · install · contribute · Gratitute ?? Palettes Rust C Lua Ruby Go sh js ?? install Installing this

BinaryBrainiacs 4 Jan 28, 2023
Generate a vanity address (`juno1wynd...`) to show your support for WYND DAO

WYND Generator When you generate a new mnemonic, it is very random (must be to be secure), and you cannot predict the address you will get. However, i

null 9 Dec 8, 2022
Here we will show you how to build a simple parser.

A Rustic invitation to parsing: Calculator demo This is the code repository that accompanies the Rustic invitation to parsing blog post. It provides a

EqualTo 5 Apr 25, 2023
App to collect ram/cpu usage from OS and show it in pretty graphs

System info collector This is simple app to collect data about system cpu and memory usage over time. After collecting results into csv file, html fil

Rafał Mikrut 3 Jul 11, 2023
Fast command-line application to show the moon phase

moon-phases Command-line application to show the moon phase for a given date and time, as a text string, emoji, or numeric value. It can also show the

mirrorwitch 3 Oct 7, 2023
Show HTML content "inside" your egui rendered application

hframe Show HTML content "inside" your egui rendered application. "hframe" stands for "HTML Frame". Note: hframe only works when the application is co

Franco Profeti 3 Feb 26, 2024
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. 125 Dec 1, 2022
Original source code for Practical Rust Projects 2nd ed. by Shing Lyu and Andrew Rzeznik

Apress Source Code This repository accompanies Practical Rust Projects 2nd ed. by Shing Lyu and Andrew Rzeznik (Apress, 2023). Download the files as a

Apress 6 Aug 13, 2023
Projects worked on during Juno Code and Chill sessions.

Juno Code and Chill projects These projects are created during Juno "Code and Chill" session in the Juno discord. Project Description cw721-piggy-bank

Junø 6 May 2, 2023