weeder: Detect dead code

[ bsd3, development, library, program ] [ Propose Tags ] [ Report a vulnerability ]

Find declarations.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1, 0.1.1, 0.1.2, 0.1.3, 0.1.4, 0.1.5, 0.1.6, 0.1.7, 0.1.8, 0.1.9, 0.1.10, 0.1.11, 0.1.12, 0.1.13, 1.0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5, 1.0.6, 1.0.7, 1.0.8, 1.0.9, 2.0.0, 2.0.1, 2.1.0, 2.1.1, 2.1.2, 2.1.3, 2.2.0, 2.3.0, 2.3.1, 2.4.0, 2.4.1, 2.5.0, 2.6.0, 2.7.0, 2.8.0, 2.9.0
Change log CHANGELOG.md
Dependencies algebraic-graphs (>=0.7 && <0.8), async (>=2.2.0 && <2.3), base (>=4.17.0.0 && <4.18 || >=4.18.0.0 && <4.19 || >=4.19.0.0 && <4.20), bytestring (>=0.10.9.0 && <0.11 || >=0.11.0.0 && <0.12 || >=0.12.0.2 && <0.13), containers (>=0.6.2.1 && <0.7), directory (>=1.3.3.2 && <1.4), filepath (>=1.4.2.1 && <1.5), generic-lens (>=2.2.0.0 && <2.3), ghc (>=9.4 && <9.5 || >=9.6 && <9.7 || >=9.8 && <9.9), Glob (>=0.9 && <0.11), lens (>=5.1 && <5.4), mtl (>=2.2.2 && <2.4), optparse-applicative (>=0.14.3.0 && <0.15 || >=0.15.1.0 && <0.16 || >=0.16.0.0 && <0.18 || >=0.18.1.0 && <0.19), parallel (>=3.2.0.0 && <3.3), regex-tdfa (>=1.2.0.0 && <1.3 || >=1.3.1.0 && <1.4), text (>=2.0.1 && <2.2), toml-reader (>=0.2.0.0 && <0.3), transformers (>=0.5.6.2 && <0.7), weeder [details]
License BSD-3-Clause
Copyright Neil Mitchell 2017-2020, Oliver Charles 2020-2024
Author Ollie Charles <ollie@ocharles.org.uk>
Maintainer Ollie Charles <ollie@ocharles.org.uk>
Category Development
Home page https://github.com/ocharles/weeder#readme
Bug tracker https://github.com/ocharles/weeder/issues
Uploaded by OliverCharles at 2024-08-10T14:08:31Z
Distributions NixOS:2.9.0
Reverse Dependencies 1 direct, 0 indirect [details]
Executables weeder
Downloads 19053 total (81 in the last 30 days)
Rating 2.5 (votes: 5) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
All reported builds failed as of 2024-08-10 [all 1 reports]

Readme for weeder-2.9.0

[back to package description]

Weeder

Weeder is an application to perform whole-program dead-code analysis. Dead code is code that is written, but never reachable from any other code. Over the lifetime of a project, this happens as code is added and removed, and leftover code is never cleaned up. While GHC has warnings to detect dead code is a single module, these warnings don't extend across module boundaries - this is where Weeder comes in.

Weeder uses HIE files produced by GHC - these files can be thought of as source code that has been enhanced by GHC, adding full symbol resolution and type information. Weeder builds a dependency graph from these files to understand how code interacts. Once all analysis is done, Weeder performs a traversal of this graph from a set of roots (e.g., your main function), and determines which code is reachable and which code is dead.

Using Weeder

Preparing Your Code for Weeder

To use Weeder, you will need to generate .hie files from your source code.

Cabal

If you use Cabal, this is easily done by adding one line to your cabal.project.local file:

program-options
  ghc-options: -fwrite-ide-info

Once this has been added, perform a full rebuild of your project:

cabal clean
cabal build all

Stack

If you use stack, add the following to your stack.yaml:

ghc-options:
  "$locals": -fwrite-ide-info

and rebuild:

stack clean
stack build

Nix

See weeder-nix for weeder <-> nixpkgs integration.

Calling Weeder

To call Weeder, you first need to provide a configuration file, weeder.toml. Weeder uses TOML as its configuration format.

roots is a list of regular expressions of symbols that are considered as alive. If you're building an executable, the pattern ^Main.main$ is a good starting point - specifying that main is a root. Weeder currently doesn't add all exported functions as roots automatically but in many cases main from a test suite could be a good workaround for that

type-class-roots configures whether or not Weeder should consider all instances of type classes as roots. Defaults to false.

roots = [ "^Main.main$" ]
type-class-roots = true

Now invoke the weeder executable, and - if your project has weeds - you will see something like the following:

$ weeder
src/Dhall/TH.hs:187: toDeclaration
src/Dhall/TH.hs:196: toNestedHaskellType

… which indicates the location of two unused symbols. (Please note these warnings are just for demonstration and not necessarily weeds in the Dhall project).

Configuration options

Name Default value Description
roots [ "Main.main", "^Paths_weeder.*" ] Any declarations matching these regular expressions will be considered as alive.
type-class-roots false Consider all instances of type classes as roots. Overrides root-instances.
root-instances [ {class = '\.IsString$'}, {class = '\.IsList$'} ] Type class instances that match on all specified fields will be considered as roots. Accepts the fields instance matching on the pretty-printed type of the instance (visible in the output), class matching on its parent class declaration, and module matching on the module the instance is defined in.
root-modules [] The exports of all matching modules will be considered as alive. This does not include type class instances implicitly exported by the module.
unused-types false Enable analysis of unused types.

root-instances can also accept string literals as a shorthand for writing a table containing only the instance field. See the following example from the test suite:

root-instances = [ { module = "Spec.ConfigInstanceModules.Module1", instance = "Bounded T" }
                 , "Read T" 
                 , { module = "Spec.ConfigInstanceModules.Module3" }
                 , { class = '\.Enum$' }
                 , { module = "Spec.ConfigInstanceModules.Module2", class = '\.Show$' }
                 ]

Exit codes

Weeder emits the following exit codes:

Exit code Cause
0 No weeds were found
228 One or more weeds found
1 Generic failing exit code
2 Failure to read HIE file due to GHC version mismatch
3 Failure to parse config file
4 No HIE files found

Tips

  • You may want to add ^Paths_.* to the roots in weeder.toml to ignore the Paths_packageName module automatically generated by Cabal.

  • You can automatically write and use a default configuration file by calling Weeder with the --write-default-config flag, if no configuration file is found.

  • You can mandate explicitly specifying every option in the configuration by calling Weeder with the --no-default-fields flag. This can prevent being caught off guard by new configuration options or changes to default values.

  • To mark all instances in a module M as roots, add { module = "^M$" } to root-instances.

Limitations

Weeder currently has a few limitations:

Overloaded syntax

On some versions of GHC, Weeder might report various type classes that are used for syntax extensions as weeds. For example, Num and IsString classes might be flagged as weeds if they are only used for overloaded literal syntax (that is, the fromInteger and fromString methods).

You can add instances of specific type classes as roots with the root-instances field, or toggle whether Weeder considers all type class instances as roots with the type-class-roots configuration option.

Type families

Weeder cannot yet analyse uses of type family instances. For this reason type family instances will be marked as implicit roots if analysis of types is enabled via unused-types.

Template Haskell

Weeder is currently unable to parse the result of a Template Haskell splice. If some Template Haskell code refers to other source code, this dependency won't be tracked by Weeder, and thus Weeder might end up with false positives.