Add `cargo lintcheck --recursive` to check dependencies of crates r? `@matthiaskrgr` This just adds the mode without changing how a regular run works Takes a fair bit longer to run than a `-j4` or regular run ``` cargo lintcheck -j4 468.33s user 43.78s system 290% cpu 2:56.42 total cargo lintcheck 428.81s user 41.85s system 199% cpu 3:55.37 total cargo lintcheck --recursive 679.83s user 45.01s system 210% cpu 5:43.72 total ``` But finds more results: <details> <summary>Stats</summary> <pre><code>clippy::explicit_counter_loop 0 => 1 clippy::needless_question_mark 0 => 3 clippy::unnecessary_cast 0 => 2 clippy::to_string_in_format_args 0 => 4 clippy::deprecated_cfg_attr 0 => 23 clippy::redundant_closure 0 => 9 clippy::drop_copy 0 => 4 clippy::double_must_use 0 => 1 clippy::transmute_num_to_bytes 0 => 9 clippy::bind_instead_of_map 0 => 14 clippy::float_cmp 0 => 16 clippy::is_digit_ascii_radix 0 => 16 clippy::manual_swap 0 => 1 clippy::needless_match 0 => 2 clippy::vec_init_then_push 0 => 1 clippy::never_loop 0 => 1 clippy::option_map_or_none 0 => 4 clippy::tabs_in_doc_comments 0 => 1 clippy::naive_bytecount 0 => 1 clippy::collapsible_if 0 => 24 clippy::copy_iterator 0 => 5 clippy::unused_io_amount 0 => 2 clippy::result_large_err 0 => 141 clippy::useless_conversion 0 => 24 clippy::flat_map_option 0 => 8 clippy::useless_format 0 => 2 clippy::module_inception 0 => 1 clippy::drop_ref 0 => 2 clippy::unnecessary_fold 0 => 2 clippy::neg_multiply 0 => 1 clippy::while_let_loop 0 => 6 clippy::missing_inline_in_public_items 0 => 37 clippy::unnecessary_mut_passed 0 => 1 unknown_lints 0 => 15 clippy::wildcard_dependencies 0 => 3 clippy::same_item_push 0 => 2 clippy::useless_asref 0 => 1 clippy::unnecessary_unwrap 0 => 4 clippy::iter_not_returning_iterator 0 => 5 clippy::comparison_to_empty 0 => 10 clippy::ref_option_ref 0 => 4 clippy::unused_peekable 0 => 1 clippy::needless_range_loop 0 => 8 clippy::absurd_extreme_comparisons 0 => 2 clippy::unnecessary_operation 0 => 2 clippy::for_kv_map 0 => 5 clippy::unnecessary_owned_empty_strings 0 => 3 clippy::transmutes_expressible_as_ptr_casts 0 => 1 clippy::toplevel_ref_arg 0 => 2 clippy::uninit_vec 0 => 3 clippy::filter_next 0 => 1 clippy::wildcard_in_or_patterns 0 => 6 clippy::cast_ptr_alignment 0 => 48 clippy::manual_memcpy 0 => 1 clippy::assign_op_pattern 0 => 313 clippy::unnecessary_lazy_evaluations 0 => 14 clippy::println_empty_string 0 => 2 clippy::redundant_pattern 0 => 2 clippy::declare_interior_mutable_const 0 => 8 clippy::large_stack_arrays 0 => 4 clippy::match_bool 0 => 4 clippy::unicode_not_nfc 0 => 2075 clippy::inconsistent_digit_grouping 0 => 4 clippy::no_effect_underscore_binding 0 => 2 clippy::let_and_return 0 => 5 clippy::transmute_ptr_to_ref 0 => 12 clippy::op_ref 0 => 13 clippy::unnecessary_join 0 => 4 clippy::into_iter_on_ref 0 => 13 clippy::from_str_radix_10 0 => 7 clippy::ptr_offset_with_cast 0 => 48 clippy::erasing_op 0 => 1 clippy::swap_ptr_to_ref 0 => 3 clippy::needless_bitwise_bool 0 => 2 clippy::extend_with_drain 0 => 19 clippy::only_used_in_recursion 0 => 4 clippy::needless_late_init 0 => 8 clippy::excessive_precision 0 => 1959 clippy::match_ref_pats 0 => 10 clippy::unit_arg 0 => 20 clippy::bool_comparison 0 => 4 clippy::bool_assert_comparison 0 => 1 clippy::eq_op 0 => 6 clippy::cast_abs_to_unsigned 0 => 6 clippy::format_in_format_args 0 => 1 clippy::iter_cloned_collect 0 => 4 clippy::ptr_eq 0 => 3 clippy::needless_bool 0 => 5 clippy::transmute_ptr_to_ptr 0 => 16 clippy::needless_option_take 0 => 2 clippy::flat_map_identity 0 => 1 clippy::needless_splitn 0 => 2 clippy::blocks_in_if_conditions 0 => 1 clippy::write_literal 0 => 1 clippy::manual_split_once 0 => 1 clippy::result_unit_err 0 => 36 clippy::unused_unit 0 => 11 clippy::single_match 0 => 22 clippy::manual_find 0 => 3 clippy::derive_ord_xor_partial_ord 0 => 6 clippy::char_lit_as_u8 0 => 2 clippy::let_unit_value 0 => 2 clippy::needless_continue 0 => 19 clippy::zero_sized_map_values 0 => 4 clippy::needless_arbitrary_self_type 0 => 6 clippy::partialeq_to_none 0 => 11 clippy::partialeq_ne_impl 0 => 1 clippy::invalid_upcast_comparisons 0 => 1 clippy::mut_range_bound 0 => 4 clippy::match_result_ok 0 => 2 clippy::ptr_arg 0 => 8 clippy::iter_nth_zero 0 => 18 clippy::needless_for_each 0 => 1 clippy::manual_unwrap_or 0 => 1 clippy::transmute_int_to_float 0 => 6 clippy::cast_slice_from_raw_parts 0 => 1 clippy::match_wild_err_arm 0 => 2 clippy::match_like_matches_macro 4 => 116 clippy::enum_glob_use 50 => 380 clippy::get_first 3 => 33 clippy::needless_doctest_main 10 => 26 clippy::struct_excessive_bools 19 => 51 clippy::cast_possible_wrap 46 => 538 clippy::manual_string_new 10 => 27 clippy::match_same_arms 53 => 1039 clippy::manual_non_exhaustive 1 => 33 clippy::redundant_pattern_matching 2 => 13 clippy::new_without_default 5 => 73 clippy::option_as_ref_deref 2 => 9 clippy::unwrap_or_else_default 2 => 4 clippy::case_sensitive_file_extension_comparisons 6 => 9 clippy::cast_precision_loss 45 => 110 clippy::needless_pass_by_value 26 => 187 clippy::redundant_closure_for_method_calls 170 => 539 clippy::let_underscore_drop 33 => 133 clippy::single_match_else 51 => 138 clippy::needless_borrow 24 => 382 clippy::redundant_else 37 => 151 clippy::type_complexity 2 => 22 clippy::ptr_as_ptr 93 => 1135 clippy::needless_lifetimes 7 => 100 clippy::single_char_add_str 2 => 22 clippy::similar_names 99 => 352 clippy::cargo_common_metadata 25 => 276 clippy::int_plus_one 1 => 2 clippy::missing_safety_doc 9 => 152 clippy::redundant_slicing 2 => 13 clippy::mut_mut 2 => 17 clippy::derive_partial_eq_without_eq 8 => 141 clippy::derive_hash_xor_eq 2 => 20 clippy::from_iter_instead_of_collect 2 => 17 clippy::verbose_bit_mask 1 => 8 clippy::too_many_lines 58 => 162 clippy::module_name_repetitions 178 => 1104 clippy::explicit_into_iter_loop 12 => 32 clippy::cast_lossless 45 => 478 clippy::many_single_char_names 9 => 23 clippy::unnested_or_patterns 27 => 127 clippy::upper_case_acronyms 5 => 29 clippy::needless_return 5 => 97 clippy::precedence 1 => 11 clippy::len_zero 2 => 70 clippy::manual_strip 2 => 30 clippy::derivable_impls 2 => 12 clippy::unused_self 20 => 187 clippy::enum_variant_names 1 => 6 clippy::self_named_constructors 1 => 3 clippy::explicit_auto_deref 19 => 314 clippy::semicolon_if_nothing_returned 137 => 1861 clippy::should_implement_trait 1 => 7 clippy::expl_impl_clone_on_copy 159 => 1318 clippy::stable_sort_primitive 4 => 12 clippy::mem_replace_with_default 7 => 46 clippy::borrow_deref_ref 5 => 140 clippy::large_enum_variant 1 => 4 clippy::map_unwrap_or 30 => 203 clippy::zero_ptr 3 => 25 clippy::filter_map_next 2 => 6 clippy::identity_op 5 => 76 clippy::checked_conversions 1 => 8 clippy::len_without_is_empty 9 => 47 clippy::missing_errors_doc 372 => 2333 clippy::fn_params_excessive_bools 3 => 7 clippy::single_component_path_imports 6 => 28 clippy::unreadable_literal 366 => 9814 clippy::field_reassign_with_default 1 => 5 clippy::redundant_clone 1 => 8 clippy::cloned_instead_of_copied 36 => 78 clippy::too_many_arguments 4 => 22 clippy::option_map_unit_fn 7 => 9 clippy::extra_unused_lifetimes 1 => 24 clippy::unnecessary_wraps 26 => 128 clippy::used_underscore_binding 1 => 50 clippy::inconsistent_struct_constructor 2 => 7 clippy::manual_range_contains 9 => 120 clippy::map_clone 7 => 46 clippy::cast_slice_different_sizes 1 => 4 clippy::missing_panics_doc 114 => 603 renamed_and_removed_lints 3 => 9 clippy::items_after_statements 155 => 309 clippy::inefficient_to_string 5 => 6 clippy::comparison_chain 1 => 19 clippy::crate_in_macro_def 3 => 6 clippy::write_with_newline 2 => 36 clippy::manual_saturating_arithmetic 1 => 2 clippy::clone_on_copy 1 => 86 clippy::negative_feature_names 3 => 16 clippy::redundant_field_names 112 => 1013 clippy::from_over_into 2 => 28 clippy::wildcard_imports 178 => 376 clippy::unusual_byte_groupings 19 => 65 clippy::option_option 2 => 10 clippy::nonminimal_bool 1 => 17 clippy::borrow_as_ptr 2 => 172 clippy::redundant_static_lifetimes 24 => 1701 clippy::or_fun_call 1 => 63 clippy::single_char_pattern 3 => 79 clippy::explicit_iter_loop 72 => 148 clippy::collapsible_else_if 2 => 27 clippy::manual_str_repeat 1 => 6 clippy::if_same_then_else 3 => 31 clippy::while_let_on_iterator 4 => 28 clippy::multiple_crate_versions 5 => 19 clippy::cast_possible_truncation 115 => 1172 clippy::explicit_deref_methods 1 => 38 clippy::default_trait_access 48 => 130 clippy::question_mark 2 => 28 clippy::must_use_candidate 612 => 5369 clippy::manual_map 1 => 12 clippy::bool_to_int_with_if 2 => 15 clippy::doc_markdown 202 => 1709 clippy::cast_sign_loss 60 => 477 clippy::wrong_self_convention 11 => 45 clippy::transmute_float_to_int 6 => 18 clippy::return_self_not_must_use 66 => 736 clippy::range_plus_one 1 => 36 clippy::manual_assert 11 => 62 clippy::trivially_copy_pass_by_ref 40 => 189 clippy::match_on_vec_items 2 => 7 clippy::inline_always 59 => 1079 clippy::if_not_else 31 => 205 clippy::implicit_clone 10 => 32 clippy::match_wildcard_for_single_variants 16 => 101 clippy::doc_link_with_quotes 7 => 35 clippy::redundant_feature_names 4 => 41 </code></pre></details> changelog: none
Clippy
A collection of lints to catch common mistakes and improve your Rust code.
There are over 550 lints included in this crate!
Lints are divided into categories, each with a default lint level.
You can choose how much Clippy is supposed to annoy help you by changing the lint level by category.
Category | Description | Default level |
---|---|---|
clippy::all |
all lints that are on by default (correctness, suspicious, style, complexity, perf) | warn/deny |
clippy::correctness |
code that is outright wrong or useless | deny |
clippy::suspicious |
code that is most likely wrong or useless | warn |
clippy::style |
code that should be written in a more idiomatic way | warn |
clippy::complexity |
code that does something simple but in a complex way | warn |
clippy::perf |
code that can be written to run faster | warn |
clippy::pedantic |
lints which are rather strict or have occasional false positives | allow |
clippy::nursery |
new lints that are still under development | allow |
clippy::cargo |
lints for the cargo manifest | allow |
More to come, please file an issue if you have ideas!
The lint list also contains "restriction lints", which are for things which are usually not considered "bad", but may be useful to turn on in specific cases. These should be used very selectively, if at all.
Table of contents:
Usage
Below are instructions on how to use Clippy as a cargo subcommand, in projects that do not use cargo, or in Travis CI.
As a cargo subcommand (cargo clippy
)
One way to use Clippy is by installing Clippy through rustup as a cargo subcommand.
Step 1: Install Rustup
You can install Rustup on supported platforms. This will help us install Clippy and its dependencies.
If you already have Rustup installed, update to ensure you have the latest Rustup and compiler:
rustup update
Step 2: Install Clippy
Once you have rustup and the latest stable release (at least Rust 1.29) installed, run the following command:
rustup component add clippy
If it says that it can't find the clippy
component, please run rustup self update
.
Step 3: Run Clippy
Now you can run Clippy by invoking the following command:
cargo clippy
Automatically applying Clippy suggestions
Clippy can automatically apply some lint suggestions, just like the compiler.
cargo clippy --fix
Workspaces
All the usual workspace options should work with Clippy. For example the following command
will run Clippy on the example
crate:
cargo clippy -p example
As with cargo check
, this includes dependencies that are members of the workspace, like path dependencies.
If you want to run Clippy only on the given crate, use the --no-deps
option like this:
cargo clippy -p example -- --no-deps
Using clippy-driver
Clippy can also be used in projects that do not use cargo. To do so, run clippy-driver
with the same arguments you use for rustc
. For example:
clippy-driver --edition 2018 -Cpanic=abort foo.rs
Note that clippy-driver
is designed for running Clippy only and should not be used as a general
replacement for rustc
. clippy-driver
may produce artifacts that are not optimized as expected,
for example.
Travis CI
You can add Clippy to Travis CI in the same way you use it locally:
language: rust
rust:
- stable
- beta
before_script:
- rustup component add clippy
script:
- cargo clippy
# if you want the build job to fail when encountering warnings, use
- cargo clippy -- -D warnings
# in order to also check tests and non-default crate features, use
- cargo clippy --all-targets --all-features -- -D warnings
- cargo test
# etc.
Note that adding -D warnings
will cause your build to fail if any warnings are found in your code.
That includes warnings found by rustc (e.g. dead_code
, etc.). If you want to avoid this and only cause
an error for Clippy warnings, use #![deny(clippy::all)]
in your code or -D clippy::all
on the command
line. (You can swap clippy::all
with the specific lint category you are targeting.)
Configuration
Allowing/denying lints
You can add options to your code to allow
/warn
/deny
Clippy lints:
-
the whole set of
Warn
lints using theclippy
lint group (#![deny(clippy::all)]
). Note thatrustc
has additional lint groups. -
all lints using both the
clippy
andclippy::pedantic
lint groups (#![deny(clippy::all)]
,#![deny(clippy::pedantic)]
). Note thatclippy::pedantic
contains some very aggressive lints prone to false positives. -
only some lints (
#![deny(clippy::single_match, clippy::box_vec)]
, etc.) -
allow
/warn
/deny
can be limited to a single function or module using#[allow(...)]
, etc.
Note: allow
means to suppress the lint for your code. With warn
the lint
will only emit a warning, while with deny
the lint will emit an error, when
triggering for your code. An error causes clippy to exit with an error code, so
is useful in scripts like CI/CD.
If you do not want to include your lint levels in your code, you can globally enable/disable lints by passing extra flags to Clippy during the run:
To allow lint_name
, run
cargo clippy -- -A clippy::lint_name
And to warn on lint_name
, run
cargo clippy -- -W clippy::lint_name
This also works with lint groups. For example, you can run Clippy with warnings for all lints enabled:
cargo clippy -- -W clippy::pedantic
If you care only about a single lint, you can allow all others and then explicitly warn on the lint(s) you are interested in:
cargo clippy -- -A clippy::all -W clippy::useless_format -W clippy::...
Configure the behavior of some lints
Some lints can be configured in a TOML file named clippy.toml
or .clippy.toml
. It contains a basic variable = value
mapping e.g.
avoid-breaking-exported-api = false
disallowed-names = ["toto", "tata", "titi"]
cognitive-complexity-threshold = 30
See the list of lints for more information about which lints can be configured and the meaning of the variables.
Note
clippy.toml
or.clippy.toml
cannot be used to allow/deny lints.
Note
Configuration changes will not apply for code that has already been compiled and cached under
./target/
; for example, adding a new string todoc-valid-idents
may still result in Clippy flagging that string. To be sure that any configuration changes are applied, you may want to runcargo clean
and re-compile your crate from scratch.
To deactivate the “for further information visit lint-link” message you can
define the CLIPPY_DISABLE_DOCS_LINKS
environment variable.
Specifying the minimum supported Rust version
Projects that intend to support old versions of Rust can disable lints pertaining to newer features by specifying the minimum supported Rust version (MSRV) in the clippy configuration file.
msrv = "1.30.0"
Alternatively, the rust-version
field
in the Cargo.toml
can be used.
# Cargo.toml
rust-version = "1.30"
The MSRV can also be specified as an inner attribute, like below.
#![feature(custom_inner_attributes)]
#![clippy::msrv = "1.30.0"]
fn main() {
...
}
You can also omit the patch version when specifying the MSRV, so msrv = 1.30
is equivalent to msrv = 1.30.0
.
Note: custom_inner_attributes
is an unstable feature, so it has to be enabled explicitly.
Lints that recognize this configuration option can be found here
Contributing
If you want to contribute to Clippy, you can find more information in CONTRIBUTING.md.
License
Copyright 2014-2022 The Rust Project Developers
Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. Files in the project may not be copied, modified, or distributed except according to those terms.