Skip to content

Commit

Permalink
Merge branch 'master' into issue-4078
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanTheMaster authored Oct 14, 2019
2 parents 7211ea0 + 8fae2dd commit de7681b
Show file tree
Hide file tree
Showing 70 changed files with 983 additions and 162 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<!--
Thank you for making Clippy better!

We're collecting our changelog from pull request descriptions.
Expand All @@ -25,6 +24,8 @@ checked during review or continuous integration.
Note that you can skip the above if you are just opening a WIP PR in
order to get feedback.

Delete this line and everything above before opening your PR -->
Delete this line and everything above before opening your PR.

---

changelog: none
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@ Released 2018-09-13
[`diverging_sub_expression`]: https://rust-lang.github.io/rust-clippy/master/index.html#diverging_sub_expression
[`doc_markdown`]: https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
[`double_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_comparisons
[`double_must_use`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_must_use
[`double_neg`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_neg
[`double_parens`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_parens
[`drop_bounds`]: https://rust-lang.github.io/rust-clippy/master/index.html#drop_bounds
Expand Down Expand Up @@ -1095,6 +1096,8 @@ Released 2018-09-13
[`modulo_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#modulo_one
[`multiple_crate_versions`]: https://rust-lang.github.io/rust-clippy/master/index.html#multiple_crate_versions
[`multiple_inherent_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#multiple_inherent_impl
[`must_use_candidate`]: https://rust-lang.github.io/rust-clippy/master/index.html#must_use_candidate
[`must_use_unit`]: https://rust-lang.github.io/rust-clippy/master/index.html#must_use_unit
[`mut_from_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_from_ref
[`mut_mut`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_mut
[`mut_range_bound`]: https://rust-lang.github.io/rust-clippy/master/index.html#mut_range_bound
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ You can use [rustup-toolchain-install-master][rtim] to do that:

```bash
cargo install rustup-toolchain-install-master
rustup-toolchain-install-master -n master --force
rustup-toolchain-install-master --force -n master
rustup override set master
cargo test
```
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ rustc_tools_util = { version = "0.2.0", path = "rustc_tools_util"}

[dev-dependencies]
cargo_metadata = "0.8.0"
compiletest_rs = { version = "0.3.23", features = ["tmp"] }
compiletest_rs = { version = "0.3.24", features = ["tmp"] }
lazy_static = "1.0"
clippy-mini-macro-test = { version = "0.2", path = "mini-macro" }
serde = { version = "1.0", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

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

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

Expand Down
6 changes: 2 additions & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ install:
- curl -sSf -o rustup-init.exe https://win.rustup.rs/
- rustup-init.exe -y --default-host %TARGET% --default-toolchain nightly
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
- git ls-remote https://github.com/rust-lang/rust.git master | awk '{print $1}' >rustc-hash.txt
- set /p RUSTC_HASH=<rustc-hash.txt
- del rust-toolchain
- cargo install rustup-toolchain-install-master --debug || echo "rustup-toolchain-install-master already installed"
- rustup-toolchain-install-master %RUSTC_HASH% -f -n master
- cargo install --git https://github.com/kennytm/rustup-toolchain-install-master --debug || echo "rustup-toolchain-install-master already installed"
- rustup-toolchain-install-master -f -n master
- rustup component add rustfmt --toolchain nightly & exit 0 # Format test handles missing rustfmt
- rustup default master
- set PATH=%PATH%;C:\Users\appveyor\.rustup\toolchains\master\bin
Expand Down
7 changes: 7 additions & 0 deletions clippy_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct Lint {
}

impl Lint {
#[must_use]
pub fn new(name: &str, group: &str, desc: &str, deprecation: Option<&str>, module: &str) -> Self {
Self {
name: name.to_lowercase(),
Expand All @@ -58,19 +59,22 @@ impl Lint {
}

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

#[must_use]
pub fn is_internal(&self) -> bool {
self.group.starts_with("internal")
}
}

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

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

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

/// Generates the `register_removed` code in `./clippy_lints/src/lib.rs`.
#[must_use]
pub fn gen_deprecated(lints: &[Lint]) -> Vec<String> {
lints
.iter()
Expand Down
1 change: 1 addition & 0 deletions clippy_dev/src/stderr_length_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn stderr_files() -> impl Iterator<Item = walkdir::DirEntry> {
.filter(|f| f.path().extension() == Some(OsStr::new("stderr")))
}

#[must_use]
fn count_linenumbers(filepath: &str) -> usize {
if let Ok(mut file) = File::open(filepath) {
let mut content = String::new();
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/approx_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ fn check_known_consts(cx: &LateContext<'_, '_>, e: &Expr, s: symbol::Symbol, mod
/// Returns `false` if the number of significant figures in `value` are
/// less than `min_digits`; otherwise, returns true if `value` is equal
/// to `constant`, rounded to the number of digits present in `value`.
#[must_use]
fn is_approx_const(constant: f64, value: &str, min_digits: usize) -> bool {
if value.len() <= min_digits {
false
Expand Down
22 changes: 1 addition & 21 deletions clippy_lints/src/assertions_on_constants.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::consts::{constant, Constant};
use crate::utils::paths;
use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, snippet_opt, span_help_and_lint};
use crate::utils::{is_direct_expn_of, is_expn_of, match_function_call, snippet_opt, span_help_and_lint};
use if_chain::if_chain;
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
Expand Down Expand Up @@ -145,23 +145,3 @@ fn match_assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx E
}
None
}

/// Matches a function call with the given path and returns the arguments.
///
/// Usage:
///
/// ```rust,ignore
/// if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC);
/// ```
fn match_function_call<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, path: &[&str]) -> Option<&'a [Expr]> {
if_chain! {
if let ExprKind::Call(ref fun, ref args) = expr.kind;
if let ExprKind::Path(ref qpath) = fun.kind;
if let Some(fun_def_id) = cx.tables.qpath_res(qpath, fun.hir_id).opt_def_id();
if match_def_path(cx, fun_def_id, path);
then {
return Some(&args)
}
};
None
}
1 change: 1 addition & 0 deletions clippy_lints/src/assign_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ fn lint_misrefactored_assign_op(
);
}

#[must_use]
fn is_commutative(op: hir::BinOpKind) -> bool {
use rustc::hir::BinOpKind::*;
match op {
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/bit_mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub struct BitMask {
}

impl BitMask {
#[must_use]
pub fn new(verbose_bit_mask_threshold: u64) -> Self {
Self {
verbose_bit_mask_threshold,
Expand Down Expand Up @@ -150,6 +151,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
}
}

#[must_use]
fn invert_cmp(cmp: BinOpKind) -> BinOpKind {
match cmp {
BinOpKind::Eq => BinOpKind::Eq,
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/checked_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ impl<'a> Conversion<'a> {

impl ConversionType {
/// Creates a conversion type if the type is allowed & conversion is valid
#[must_use]
fn try_new(from: &str, to: &str) -> Option<Self> {
if UINTS.contains(&from) {
Some(Self::FromUnsigned)
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/cognitive_complexity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct CognitiveComplexity {
}

impl CognitiveComplexity {
#[must_use]
pub fn new(limit: u64) -> Self {
Self {
limit: LimitStack::new(limit),
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DocMarkdown {
/// need to keep track of
/// the spans but this function is inspired from the later.
#[allow(clippy::cast_possible_truncation)]
#[must_use]
pub fn strip_doc_comment_decoration(comment: &str, span: Span) -> (String, Vec<(usize, Span)>) {
// one-line comments lose their prefix
const ONELINERS: &[&str] = &["///!", "///", "//!", "//"];
Expand Down
4 changes: 4 additions & 0 deletions clippy_lints/src/enum_variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub struct EnumVariantNames {
}

impl EnumVariantNames {
#[must_use]
pub fn new(threshold: u64) -> Self {
Self {
modules: Vec::new(),
Expand All @@ -123,13 +124,15 @@ impl_lint_pass!(EnumVariantNames => [
]);

/// Returns the number of chars that match from the start
#[must_use]
fn partial_match(pre: &str, name: &str) -> usize {
let mut name_iter = name.chars();
let _ = name_iter.next_back(); // make sure the name is never fully matched
pre.chars().zip(name_iter).take_while(|&(l, r)| l == r).count()
}

/// Returns the number of chars that match from the end
#[must_use]
fn partial_rmatch(post: &str, name: &str) -> usize {
let mut name_iter = name.chars();
let _ = name_iter.next(); // make sure the name is never fully matched
Expand Down Expand Up @@ -211,6 +214,7 @@ fn check_variant(
);
}

#[must_use]
fn to_camel_case(item_name: &str) -> String {
let mut s = String::new();
let mut up = true;
Expand Down
5 changes: 5 additions & 0 deletions clippy_lints/src/excessive_precision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {

impl ExcessivePrecision {
// None if nothing to lint, Some(suggestion) if lint necessary
#[must_use]
fn check(self, sym: Symbol, fty: FloatTy) -> Option<String> {
let max = max_digits(fty);
let sym_str = sym.as_str();
Expand Down Expand Up @@ -97,6 +98,7 @@ impl ExcessivePrecision {
/// Should we exclude the float because it has a `.0` or `.` suffix
/// Ex `1_000_000_000.0`
/// Ex `1_000_000_000.`
#[must_use]
fn dot_zero_exclusion(s: &str) -> bool {
s.split('.').nth(1).map_or(false, |after_dec| {
let mut decpart = after_dec.chars().take_while(|c| *c != 'e' || *c != 'E');
Expand All @@ -109,6 +111,7 @@ fn dot_zero_exclusion(s: &str) -> bool {
})
}

#[must_use]
fn max_digits(fty: FloatTy) -> u32 {
match fty {
FloatTy::F32 => f32::DIGITS,
Expand All @@ -117,6 +120,7 @@ fn max_digits(fty: FloatTy) -> u32 {
}

/// Counts the digits excluding leading zeros
#[must_use]
fn count_digits(s: &str) -> usize {
// Note that s does not contain the f32/64 suffix, and underscores have been stripped
s.chars()
Expand All @@ -138,6 +142,7 @@ enum FloatFormat {
Normal,
}
impl FloatFormat {
#[must_use]
fn new(s: &str) -> Self {
s.chars()
.find_map(|x| match x {
Expand Down
9 changes: 3 additions & 6 deletions clippy_lints/src/explicit_write.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{is_expn_of, match_def_path, paths, resolve_node, span_lint, span_lint_and_sugg};
use crate::utils::{is_expn_of, match_function_call, paths, span_lint, span_lint_and_sugg};
use if_chain::if_chain;
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
Expand Down Expand Up @@ -41,12 +41,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExplicitWrite {
if write_fun.ident.name == sym!(write_fmt);
// match calls to std::io::stdout() / std::io::stderr ()
if write_args.len() > 0;
if let ExprKind::Call(ref dest_fun, _) = write_args[0].kind;
if let ExprKind::Path(ref qpath) = dest_fun.kind;
if let Some(dest_fun_id) = resolve_node(cx, qpath, dest_fun.hir_id).opt_def_id();
if let Some(dest_name) = if match_def_path(cx, dest_fun_id, &paths::STDOUT) {
if let Some(dest_name) = if match_function_call(cx, &write_args[0], &paths::STDOUT).is_some() {
Some("stdout")
} else if match_def_path(cx, dest_fun_id, &paths::STDERR) {
} else if match_function_call(cx, &write_args[0], &paths::STDERR).is_some() {
Some("stderr")
} else {
None
Expand Down
22 changes: 7 additions & 15 deletions clippy_lints/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::utils::paths;
use crate::utils::{
is_expn_of, last_path_segment, match_def_path, match_type, resolve_node, snippet, span_lint_and_then, walk_ptrs_ty,
is_expn_of, last_path_segment, match_def_path, match_function_call, match_type, snippet, span_lint_and_then,
walk_ptrs_ty,
};
use if_chain::if_chain;
use rustc::hir::*;
Expand Down Expand Up @@ -70,19 +71,16 @@ fn span_useless_format<T: LintContext>(cx: &T, span: Span, help: &str, mut sugg:
});
}

fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arms: &'a [Arm]) -> Option<String> {
fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arms: &'tcx [Arm]) -> Option<String> {
if_chain! {
if let ExprKind::AddrOf(_, ref format_args) = expr.kind;
if let ExprKind::Array(ref elems) = arms[0].body.kind;
if elems.len() == 1;
if let ExprKind::Call(ref fun, ref args) = elems[0].kind;
if let ExprKind::Path(ref qpath) = fun.kind;
if let Some(did) = resolve_node(cx, qpath, fun.hir_id).opt_def_id();
if match_def_path(cx, did, &paths::FMT_ARGUMENTV1_NEW);
if let Some(args) = match_function_call(cx, &elems[0], &paths::FMT_ARGUMENTV1_NEW);
// matches `core::fmt::Display::fmt`
if args.len() == 2;
if let ExprKind::Path(ref qpath) = args[1].kind;
if let Some(did) = resolve_node(cx, qpath, args[1].hir_id).opt_def_id();
if let Some(did) = cx.tables.qpath_res(qpath, args[1].hir_id).opt_def_id();
if match_def_path(cx, did, &paths::DISPLAY_FMT_METHOD);
// check `(arg0,)` in match block
if let PatKind::Tuple(ref pats, None) = arms[0].pat.kind;
Expand Down Expand Up @@ -114,11 +112,8 @@ fn on_argumentv1_new<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, arm

fn on_new_v1<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<String> {
if_chain! {
if let ExprKind::Call(ref fun, ref args) = expr.kind;
if let Some(args) = match_function_call(cx, expr, &paths::FMT_ARGUMENTS_NEW_V1);
if args.len() == 2;
if let ExprKind::Path(ref qpath) = fun.kind;
if let Some(did) = resolve_node(cx, qpath, fun.hir_id).opt_def_id();
if match_def_path(cx, did, &paths::FMT_ARGUMENTS_NEW_V1);
// Argument 1 in `new_v1()`
if let ExprKind::AddrOf(_, ref arr) = args[0].kind;
if let ExprKind::Array(ref pieces) = arr.kind;
Expand All @@ -144,11 +139,8 @@ fn on_new_v1<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<S

fn on_new_v1_fmt<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<String> {
if_chain! {
if let ExprKind::Call(ref fun, ref args) = expr.kind;
if let Some(args) = match_function_call(cx, expr, &paths::FMT_ARGUMENTS_NEW_V1_FORMATTED);
if args.len() == 3;
if let ExprKind::Path(ref qpath) = fun.kind;
if let Some(did) = resolve_node(cx, qpath, fun.hir_id).opt_def_id();
if match_def_path(cx, did, &paths::FMT_ARGUMENTS_NEW_V1_FORMATTED);
if check_unformatted(&args[2]);
// Argument 1 in `new_v1_formatted()`
if let ExprKind::AddrOf(_, ref arr) = args[0].kind;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
}
}

#[must_use]
fn has_unary_equivalent(bin_op: BinOpKind) -> bool {
// &, *, -
bin_op == BinOpKind::And || bin_op == BinOpKind::Mul || bin_op == BinOpKind::Sub
Expand Down
Loading

0 comments on commit de7681b

Please sign in to comment.