Skip to content

Commit 302d2e5

Browse files
committed
submodules: update clippy from 8b7f7e6 to 329923e
Changes: ```` Apply suggestions from code review Simplify if_chain. Move NumericLiteral to its own module. Included binary and octal cases. Resolve false positives for hex int cast. Test for unnecessary_cast of hex int literal. run-rustfix Lint `if let Some` in question_mark lint Add restrictive pat use in full binded struct Update test case answers to match cargo dev fmt Ran cargo dev fmt Rustup to rust-lang/rust#69506 Recommended changes from flip1995 Revive rls integration test use question mark operator Add regression test Use `try_eval_usize` over `eval_usize` Add path for display trait Use lang items instead of get_trait_def_id where possible Update stderr Don't lint debug formatting in debug impl Whitelist unused attribute for use items. Fix one last test issue Refactor suggested by krishna-veerareddy Fixed compile error from merging Changed test output to reflect cargo fmt Run cargo dev fmt Finished checking for cases of absolute values add test for rust-lang#5238 Some bugfixing Created floating point abs lint and test, but not yet run ````
1 parent 5fc2ac4 commit 302d2e5

Some content is hidden

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

43 files changed

+1289
-435
lines changed

.github/workflows/clippy_bors.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ jobs:
231231
matrix:
232232
integration:
233233
- 'rust-lang/cargo'
234-
# FIXME: Re-enable once we can test with rls again.
235-
# - 'rust-lang/rls'
234+
- 'rust-lang/rls'
236235
- 'rust-lang/chalk'
237236
- 'rust-lang/rustfmt'
238237
- 'Marwes/combine'

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,7 @@ Released 2018-09-13
13211321
[`ref_in_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_in_deref
13221322
[`regex_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#regex_macro
13231323
[`replace_consts`]: https://rust-lang.github.io/rust-clippy/master/index.html#replace_consts
1324+
[`rest_pat_in_fully_bound_structs`]: https://rust-lang.github.io/rust-clippy/master/index.html#rest_pat_in_fully_bound_structs
13241325
[`result_expect_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_expect_used
13251326
[`result_map_unit_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_map_unit_fn
13261327
[`result_map_unwrap_or_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_map_unwrap_or_else

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

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

8-
[There are 358 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
8+
[There are 359 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
99

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

clippy_lints/src/attrs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Attributes {
248248
if is_word(lint, sym!(unused_imports))
249249
|| is_word(lint, sym!(deprecated))
250250
|| is_word(lint, sym!(unreachable_pub))
251+
|| is_word(lint, sym!(unused))
251252
{
252253
return;
253254
}

clippy_lints/src/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
231231
ExprKind::Tup(ref tup) => self.multi(tup).map(Constant::Tuple),
232232
ExprKind::Repeat(ref value, _) => {
233233
let n = match self.tables.expr_ty(e).kind {
234-
ty::Array(_, n) => n.eval_usize(self.lcx.tcx, self.lcx.param_env),
234+
ty::Array(_, n) => n.try_eval_usize(self.lcx.tcx, self.lcx.param_env)?,
235235
_ => span_bug!(e.span, "typeck error"),
236236
};
237237
self.expr(value).map(|v| Constant::Repeat(Box::new(v), n))

clippy_lints/src/doc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{get_trait_def_id, implements_trait, is_entrypoint_fn, match_type, paths, return_ty, span_lint};
1+
use crate::utils::{implements_trait, is_entrypoint_fn, match_type, paths, return_ty, span_lint};
22
use if_chain::if_chain;
33
use itertools::Itertools;
44
use rustc::lint::in_external_macro;
@@ -227,7 +227,7 @@ fn lint_for_missing_headers<'a, 'tcx>(
227227
} else {
228228
if_chain! {
229229
if let Some(body_id) = body_id;
230-
if let Some(future) = get_trait_def_id(cx, &paths::FUTURE);
230+
if let Some(future) = cx.tcx.lang_items().future_trait();
231231
let def_id = cx.tcx.hir().body_owner_def_id(body_id);
232232
let mir = cx.tcx.optimized_mir(def_id);
233233
let ret_ty = mir.return_ty();

clippy_lints/src/float_literal.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::utils::span_lint_and_sugg;
2-
use crate::utils::sugg::format_numeric_literal;
1+
use crate::utils::{numeric_literal, span_lint_and_sugg};
32
use if_chain::if_chain;
43
use rustc::ty;
54
use rustc_ast::ast::{FloatTy, LitFloatType, LitKind};
@@ -109,7 +108,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatLiteral {
109108
expr.span,
110109
"literal cannot be represented as the underlying type without loss of precision",
111110
"consider changing the type or replacing it with",
112-
format_numeric_literal(&float_str, type_suffix, true),
111+
numeric_literal::format(&float_str, type_suffix, true),
113112
Applicability::MachineApplicable,
114113
);
115114
}
@@ -120,7 +119,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatLiteral {
120119
expr.span,
121120
"float has excessive precision",
122121
"consider changing the type or truncating it to",
123-
format_numeric_literal(&float_str, type_suffix, true),
122+
numeric_literal::format(&float_str, type_suffix, true),
124123
Applicability::MachineApplicable,
125124
);
126125
}

clippy_lints/src/floating_point_arithmetic.rs

+127-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::consts::{
2-
constant, Constant,
2+
constant, constant_simple, Constant,
33
Constant::{F32, F64},
44
};
5-
use crate::utils::{span_lint_and_sugg, sugg};
5+
use crate::utils::{higher, numeric_literal, span_lint_and_sugg, sugg, SpanlessEq};
66
use if_chain::if_chain;
77
use rustc::ty;
88
use rustc_errors::Applicability;
@@ -14,7 +14,7 @@ use rustc_span::source_map::Spanned;
1414
use rustc_ast::ast;
1515
use std::f32::consts as f32_consts;
1616
use std::f64::consts as f64_consts;
17-
use sugg::{format_numeric_literal, Sugg};
17+
use sugg::Sugg;
1818

1919
declare_clippy_lint! {
2020
/// **What it does:** Looks for floating-point expressions that
@@ -72,6 +72,16 @@ declare_clippy_lint! {
7272
/// let _ = a.log(E);
7373
/// let _ = a.powf(2.0);
7474
/// let _ = a * 2.0 + 4.0;
75+
/// let _ = if a < 0.0 {
76+
/// -a
77+
/// } else {
78+
/// a
79+
/// };
80+
/// let _ = if a < 0.0 {
81+
/// a
82+
/// } else {
83+
/// -a
84+
/// };
7585
/// ```
7686
///
7787
/// is better expressed as
@@ -88,6 +98,8 @@ declare_clippy_lint! {
8898
/// let _ = a.ln();
8999
/// let _ = a.powi(2);
90100
/// let _ = a.mul_add(2.0, 4.0);
101+
/// let _ = a.abs();
102+
/// let _ = -a.abs();
91103
/// ```
92104
pub SUBOPTIMAL_FLOPS,
93105
nursery,
@@ -264,7 +276,7 @@ fn check_powf(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
264276
format!(
265277
"{}.powi({})",
266278
Sugg::hir(cx, &args[0], ".."),
267-
format_numeric_literal(&exponent.to_string(), None, false)
279+
numeric_literal::format(&exponent.to_string(), None, false)
268280
),
269281
)
270282
} else {
@@ -359,6 +371,116 @@ fn check_mul_add(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
359371
}
360372
}
361373

374+
/// Returns true iff expr is an expression which tests whether or not
375+
/// test is positive or an expression which tests whether or not test
376+
/// is nonnegative.
377+
/// Used for check-custom-abs function below
378+
fn is_testing_positive(cx: &LateContext<'_, '_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool {
379+
if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind {
380+
match op {
381+
BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, right) && are_exprs_equal(cx, left, test),
382+
BinOpKind::Lt | BinOpKind::Le => is_zero(cx, left) && are_exprs_equal(cx, right, test),
383+
_ => false,
384+
}
385+
} else {
386+
false
387+
}
388+
}
389+
390+
/// See [`is_testing_positive`]
391+
fn is_testing_negative(cx: &LateContext<'_, '_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool {
392+
if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind {
393+
match op {
394+
BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, left) && are_exprs_equal(cx, right, test),
395+
BinOpKind::Lt | BinOpKind::Le => is_zero(cx, right) && are_exprs_equal(cx, left, test),
396+
_ => false,
397+
}
398+
} else {
399+
false
400+
}
401+
}
402+
403+
fn are_exprs_equal(cx: &LateContext<'_, '_>, expr1: &Expr<'_>, expr2: &Expr<'_>) -> bool {
404+
SpanlessEq::new(cx).ignore_fn().eq_expr(expr1, expr2)
405+
}
406+
407+
/// Returns true iff expr is some zero literal
408+
fn is_zero(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
409+
match constant_simple(cx, cx.tables, expr) {
410+
Some(Constant::Int(i)) => i == 0,
411+
Some(Constant::F32(f)) => f == 0.0,
412+
Some(Constant::F64(f)) => f == 0.0,
413+
_ => false,
414+
}
415+
}
416+
417+
/// If the two expressions are negations of each other, then it returns
418+
/// a tuple, in which the first element is true iff expr1 is the
419+
/// positive expressions, and the second element is the positive
420+
/// one of the two expressions
421+
/// If the two expressions are not negations of each other, then it
422+
/// returns None.
423+
fn are_negated<'a>(cx: &LateContext<'_, '_>, expr1: &'a Expr<'a>, expr2: &'a Expr<'a>) -> Option<(bool, &'a Expr<'a>)> {
424+
if let ExprKind::Unary(UnOp::UnNeg, expr1_negated) = &expr1.kind {
425+
if are_exprs_equal(cx, expr1_negated, expr2) {
426+
return Some((false, expr2));
427+
}
428+
}
429+
if let ExprKind::Unary(UnOp::UnNeg, expr2_negated) = &expr2.kind {
430+
if are_exprs_equal(cx, expr1, expr2_negated) {
431+
return Some((true, expr1));
432+
}
433+
}
434+
None
435+
}
436+
437+
fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
438+
if_chain! {
439+
if let Some((cond, body, Some(else_body))) = higher::if_block(&expr);
440+
if let ExprKind::Block(block, _) = body.kind;
441+
if block.stmts.is_empty();
442+
if let Some(if_body_expr) = block.expr;
443+
if let ExprKind::Block(else_block, _) = else_body.kind;
444+
if else_block.stmts.is_empty();
445+
if let Some(else_body_expr) = else_block.expr;
446+
if let Some((if_expr_positive, body)) = are_negated(cx, if_body_expr, else_body_expr);
447+
then {
448+
let positive_abs_sugg = (
449+
"manual implementation of `abs` method",
450+
format!("{}.abs()", Sugg::hir(cx, body, "..")),
451+
);
452+
let negative_abs_sugg = (
453+
"manual implementation of negation of `abs` method",
454+
format!("-{}.abs()", Sugg::hir(cx, body, "..")),
455+
);
456+
let sugg = if is_testing_positive(cx, cond, body) {
457+
if if_expr_positive {
458+
positive_abs_sugg
459+
} else {
460+
negative_abs_sugg
461+
}
462+
} else if is_testing_negative(cx, cond, body) {
463+
if if_expr_positive {
464+
negative_abs_sugg
465+
} else {
466+
positive_abs_sugg
467+
}
468+
} else {
469+
return;
470+
};
471+
span_lint_and_sugg(
472+
cx,
473+
SUBOPTIMAL_FLOPS,
474+
expr.span,
475+
sugg.0,
476+
"try",
477+
sugg.1,
478+
Applicability::MachineApplicable,
479+
);
480+
}
481+
}
482+
}
483+
362484
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
363485
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
364486
if let ExprKind::MethodCall(ref path, _, args) = &expr.kind {
@@ -375,6 +497,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
375497
} else {
376498
check_expm1(cx, expr);
377499
check_mul_add(cx, expr);
500+
check_custom_abs(cx, expr);
378501
}
379502
}
380503
}

clippy_lints/src/indexing_slicing.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
9292
if let Some(range) = higher::range(cx, index) {
9393
// Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
9494
if let ty::Array(_, s) = ty.kind {
95-
let size: u128 = s.eval_usize(cx.tcx, cx.param_env).into();
95+
let size: u128 = if let Some(size) = s.try_eval_usize(cx.tcx, cx.param_env) {
96+
size.into()
97+
} else {
98+
return;
99+
};
96100

97101
let const_range = to_const_range(cx, range, size);
98102

clippy_lints/src/inherent_to_string.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InherentToString {
120120
}
121121

122122
fn show_lint(cx: &LateContext<'_, '_>, item: &ImplItem<'_>) {
123-
let display_trait_id =
124-
get_trait_def_id(cx, &["core", "fmt", "Display"]).expect("Failed to get trait ID of `Display`!");
123+
let display_trait_id = get_trait_def_id(cx, &paths::DISPLAY_TRAIT).expect("Failed to get trait ID of `Display`!");
125124

126125
// Get the real type of 'self'
127126
let fn_def_id = cx.tcx.hir().local_def_id(item.hir_id);

clippy_lints/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ mod reexport {
330330
///
331331
/// Used in `./src/driver.rs`.
332332
pub fn register_pre_expansion_lints(store: &mut rustc_lint::LintStore, conf: &Conf) {
333-
store.register_pre_expansion_pass(|| box write::Write);
333+
store.register_pre_expansion_pass(|| box write::Write::default());
334334
store.register_pre_expansion_pass(|| box redundant_field_names::RedundantFieldNames);
335335
let single_char_binding_names_threshold = conf.single_char_binding_names_threshold;
336336
store.register_pre_expansion_pass(move || box non_expressive_names::NonExpressiveNames {
@@ -610,6 +610,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
610610
&matches::MATCH_REF_PATS,
611611
&matches::MATCH_SINGLE_BINDING,
612612
&matches::MATCH_WILD_ERR_ARM,
613+
&matches::REST_PAT_IN_FULLY_BOUND_STRUCTS,
613614
&matches::SINGLE_MATCH,
614615
&matches::SINGLE_MATCH_ELSE,
615616
&matches::WILDCARD_ENUM_MATCH_ARM,
@@ -1026,6 +1027,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10261027
LintId::of(&integer_division::INTEGER_DIVISION),
10271028
LintId::of(&let_underscore::LET_UNDERSCORE_MUST_USE),
10281029
LintId::of(&literal_representation::DECIMAL_LITERAL_REPRESENTATION),
1030+
LintId::of(&matches::REST_PAT_IN_FULLY_BOUND_STRUCTS),
10291031
LintId::of(&matches::WILDCARD_ENUM_MATCH_ARM),
10301032
LintId::of(&mem_forget::MEM_FORGET),
10311033
LintId::of(&methods::CLONE_ON_REF_PTR),

0 commit comments

Comments
 (0)