Skip to content

Commit cc62260

Browse files
committed
new lints around #[must_use] fns
`must_use_unit` lints unit-returning functions with a `#[must_use]` attribute, suggesting to remove it. `double_must_use` lints functions with a plain `#[must_use]` attribute, but which return a type which is already `#[must_use]`, so the attribute has no benefit. `must_use_candidate` is a pedantic lint that lints functions and methods that return some non-unit type that is not already `#[must_use]` and suggests to add the annotation.
1 parent a865d0a commit cc62260

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+924
-88
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,7 @@ Released 2018-09-13
976976
[`diverging_sub_expression`]: https://rust-lang.github.io/rust-clippy/master/index.html#diverging_sub_expression
977977
[`doc_markdown`]: https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
978978
[`double_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_comparisons
979+
[`double_must_use`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_must_use
979980
[`double_neg`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_neg
980981
[`double_parens`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_parens
981982
[`drop_bounds`]: https://rust-lang.github.io/rust-clippy/master/index.html#drop_bounds
@@ -1095,6 +1096,8 @@ Released 2018-09-13
10951096
[`modulo_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#modulo_one
10961097
[`multiple_crate_versions`]: https://rust-lang.github.io/rust-clippy/master/index.html#multiple_crate_versions
10971098
[`multiple_inherent_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#multiple_inherent_impl
1099+
[`must_use_candidate`]: https://rust-lang.github.io/rust-clippy/master/index.html#must_use_candidate
1100+
[`must_use_unit`]: https://rust-lang.github.io/rust-clippy/master/index.html#must_use_unit
10981101
[`mut_from_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_from_ref
10991102
[`mut_mut`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_mut
11001103
[`mut_range_bound`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_range_bound

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are 321 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 324 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1212

clippy_dev/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub struct Lint {
4242
}
4343

4444
impl Lint {
45+
#[must_use]
4546
pub fn new(name: &str, group: &str, desc: &str, deprecation: Option<&str>, module: &str) -> Self {
4647
Self {
4748
name: name.to_lowercase(),
@@ -58,19 +59,22 @@ impl Lint {
5859
}
5960

6061
/// Returns the lints in a `HashMap`, grouped by the different lint groups
62+
#[must_use]
6163
pub fn by_lint_group(lints: &[Self]) -> HashMap<String, Vec<Self>> {
6264
lints
6365
.iter()
6466
.map(|lint| (lint.group.to_string(), lint.clone()))
6567
.into_group_map()
6668
}
6769

70+
#[must_use]
6871
pub fn is_internal(&self) -> bool {
6972
self.group.starts_with("internal")
7073
}
7174
}
7275

7376
/// Generates the Vec items for `register_lint_group` calls in `clippy_lints/src/lib.rs`.
77+
#[must_use]
7478
pub fn gen_lint_group_list(lints: Vec<Lint>) -> Vec<String> {
7579
lints
7680
.into_iter()
@@ -86,6 +90,7 @@ pub fn gen_lint_group_list(lints: Vec<Lint>) -> Vec<String> {
8690
}
8791

8892
/// Generates the `pub mod module_name` list in `clippy_lints/src/lib.rs`.
93+
#[must_use]
8994
pub fn gen_modules_list(lints: Vec<Lint>) -> Vec<String> {
9095
lints
9196
.into_iter()
@@ -103,6 +108,7 @@ pub fn gen_modules_list(lints: Vec<Lint>) -> Vec<String> {
103108
}
104109

105110
/// Generates the list of lint links at the bottom of the README
111+
#[must_use]
106112
pub fn gen_changelog_lint_list(lints: Vec<Lint>) -> Vec<String> {
107113
let mut lint_list_sorted: Vec<Lint> = lints;
108114
lint_list_sorted.sort_by_key(|l| l.name.clone());
@@ -119,6 +125,7 @@ pub fn gen_changelog_lint_list(lints: Vec<Lint>) -> Vec<String> {
119125
}
120126

121127
/// Generates the `register_removed` code in `./clippy_lints/src/lib.rs`.
128+
#[must_use]
122129
pub fn gen_deprecated(lints: &[Lint]) -> Vec<String> {
123130
lints
124131
.iter()

clippy_dev/src/stderr_length_check.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ fn stderr_files() -> impl Iterator<Item = walkdir::DirEntry> {
4242
.filter(|f| f.path().extension() == Some(OsStr::new("stderr")))
4343
}
4444

45+
#[must_use]
4546
fn count_linenumbers(filepath: &str) -> usize {
4647
if let Ok(mut file) = File::open(filepath) {
4748
let mut content = String::new();

clippy_lints/src/approx_const.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ fn check_known_consts(cx: &LateContext<'_, '_>, e: &Expr, s: symbol::Symbol, mod
9393
/// Returns `false` if the number of significant figures in `value` are
9494
/// less than `min_digits`; otherwise, returns true if `value` is equal
9595
/// to `constant`, rounded to the number of digits present in `value`.
96+
#[must_use]
9697
fn is_approx_const(constant: f64, value: &str, min_digits: usize) -> bool {
9798
if value.len() <= min_digits {
9899
false

clippy_lints/src/assign_ops.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ fn lint_misrefactored_assign_op(
229229
);
230230
}
231231

232+
#[must_use]
232233
fn is_commutative(op: hir::BinOpKind) -> bool {
233234
use rustc::hir::BinOpKind::*;
234235
match op {

clippy_lints/src/bit_mask.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub struct BitMask {
100100
}
101101

102102
impl BitMask {
103+
#[must_use]
103104
pub fn new(verbose_bit_mask_threshold: u64) -> Self {
104105
Self {
105106
verbose_bit_mask_threshold,
@@ -150,6 +151,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
150151
}
151152
}
152153

154+
#[must_use]
153155
fn invert_cmp(cmp: BinOpKind) -> BinOpKind {
154156
match cmp {
155157
BinOpKind::Eq => BinOpKind::Eq,

clippy_lints/src/checked_conversions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ impl<'a> Conversion<'a> {
160160

161161
impl ConversionType {
162162
/// Creates a conversion type if the type is allowed & conversion is valid
163+
#[must_use]
163164
fn try_new(from: &str, to: &str) -> Option<Self> {
164165
if UINTS.contains(&from) {
165166
Some(Self::FromUnsigned)

clippy_lints/src/cognitive_complexity.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub struct CognitiveComplexity {
2929
}
3030

3131
impl CognitiveComplexity {
32+
#[must_use]
3233
pub fn new(limit: u64) -> Self {
3334
Self {
3435
limit: LimitStack::new(limit),

clippy_lints/src/doc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DocMarkdown {
191191
/// need to keep track of
192192
/// the spans but this function is inspired from the later.
193193
#[allow(clippy::cast_possible_truncation)]
194+
#[must_use]
194195
pub fn strip_doc_comment_decoration(comment: &str, span: Span) -> (String, Vec<(usize, Span)>) {
195196
// one-line comments lose their prefix
196197
const ONELINERS: &[&str] = &["///!", "///", "//!", "//"];

0 commit comments

Comments
 (0)