Skip to content

Commit 8443444

Browse files
committed
submodules: update clippy from 280069d to f175352
Changes: ```` Rustup allow assertions_on_constants for collapsible_if and missing_test_files Improving comments. Added rustfix to the test. Improve span shortening. Added "make_return" and "blockify" convenience methods in Sugg and used them in "needless_bool". Fixed potential mistakes with nesting. Added tests. Fix automatic suggestion on `use_self`. run ./util/dev update_lints add assert(true/false, some message) tests change assert_checks to assertions_on_constants run ./util/dev update_lints Add unreachable!() as option Add assert(true) and assert(false) lints ````
1 parent 9c7fce0 commit 8443444

19 files changed

+566
-46
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ All notable changes to this project will be documented in this file.
616616
[`absurd_extreme_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons
617617
[`almost_swapped`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_swapped
618618
[`approx_constant`]: https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant
619+
[`assertions_on_constants`]: https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants
619620
[`assign_op_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#assign_op_pattern
620621
[`assign_ops`]: https://rust-lang.github.io/rust-clippy/master/index.html#assign_ops
621622
[`bad_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#bad_bit_mask

README.md

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

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

10-
[There are 291 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
10+
[There are 292 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1111

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use crate::consts::{constant, Constant};
2+
use crate::rustc::hir::{Expr, ExprKind};
3+
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4+
use crate::rustc::{declare_tool_lint, lint_array};
5+
use crate::syntax::ast::LitKind;
6+
use crate::utils::{is_direct_expn_of, span_help_and_lint};
7+
use if_chain::if_chain;
8+
9+
/// **What it does:** Check to call assert!(true/false)
10+
///
11+
/// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a
12+
/// panic!() or unreachable!()
13+
///
14+
/// **Known problems:** None
15+
///
16+
/// **Example:**
17+
/// ```rust
18+
/// assert!(false)
19+
/// // or
20+
/// assert!(true)
21+
/// // or
22+
/// const B: bool = false;
23+
/// assert!(B)
24+
/// ```
25+
declare_clippy_lint! {
26+
pub ASSERTIONS_ON_CONSTANTS,
27+
style,
28+
"assert!(true/false) will be optimized out by the compiler/should probably be replaced by a panic!() or unreachable!()"
29+
}
30+
31+
pub struct AssertionsOnConstants;
32+
33+
impl LintPass for AssertionsOnConstants {
34+
fn get_lints(&self) -> LintArray {
35+
lint_array![ASSERTIONS_ON_CONSTANTS]
36+
}
37+
}
38+
39+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
40+
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
41+
if_chain! {
42+
if is_direct_expn_of(e.span, "assert").is_some();
43+
if let ExprKind::Unary(_, ref lit) = e.node;
44+
then {
45+
if let ExprKind::Lit(ref inner) = lit.node {
46+
match inner.node {
47+
LitKind::Bool(true) => {
48+
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
49+
"assert!(true) will be optimized out by the compiler",
50+
"remove it");
51+
},
52+
LitKind::Bool(false) => {
53+
span_help_and_lint(
54+
cx, ASSERTIONS_ON_CONSTANTS, e.span,
55+
"assert!(false) should probably be replaced",
56+
"use panic!() or unreachable!()");
57+
},
58+
_ => (),
59+
}
60+
} else if let Some(bool_const) = constant(cx, cx.tables, lit) {
61+
match bool_const.0 {
62+
Constant::Bool(true) => {
63+
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
64+
"assert!(const: true) will be optimized out by the compiler",
65+
"remove it");
66+
},
67+
Constant::Bool(false) => {
68+
span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
69+
"assert!(const: false) should probably be replaced",
70+
"use panic!() or unreachable!()");
71+
},
72+
_ => (),
73+
}
74+
}
75+
}
76+
}
77+
}
78+
}

clippy_lints/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ mod utils;
7878
// begin lints modules, do not remove this comment, it’s used in `update_lints`
7979
pub mod approx_const;
8080
pub mod arithmetic;
81+
pub mod assertions_on_constants;
8182
pub mod assign_ops;
8283
pub mod attrs;
8384
pub mod bit_mask;
@@ -477,6 +478,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
477478
reg.register_late_lint_pass(box redundant_clone::RedundantClone);
478479
reg.register_late_lint_pass(box slow_vector_initialization::Pass);
479480
reg.register_late_lint_pass(box types::RefToMut);
481+
reg.register_late_lint_pass(box assertions_on_constants::AssertionsOnConstants);
480482

481483
reg.register_lint_group("clippy::restriction", Some("clippy_restriction"), vec![
482484
arithmetic::FLOAT_ARITHMETIC,
@@ -554,6 +556,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
554556

555557
reg.register_lint_group("clippy::all", Some("clippy"), vec![
556558
approx_const::APPROX_CONSTANT,
559+
assertions_on_constants::ASSERTIONS_ON_CONSTANTS,
557560
assign_ops::ASSIGN_OP_PATTERN,
558561
assign_ops::MISREFACTORED_ASSIGN_OP,
559562
attrs::DEPRECATED_CFG_ATTR,
@@ -776,6 +779,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
776779
]);
777780

778781
reg.register_lint_group("clippy::style", Some("clippy_style"), vec![
782+
assertions_on_constants::ASSERTIONS_ON_CONSTANTS,
779783
assign_ops::ASSIGN_OP_PATTERN,
780784
attrs::UNKNOWN_CLIPPY_LINTS,
781785
bit_mask::VERBOSE_BIT_MASK,

clippy_lints/src/needless_bool.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
7070
let reduce = |ret, not| {
7171
let mut applicability = Applicability::MachineApplicable;
7272
let snip = Sugg::hir_with_applicability(cx, pred, "<predicate>", &mut applicability);
73-
let snip = if not { !snip } else { snip };
73+
let mut snip = if not { !snip } else { snip };
7474

75-
let mut hint = if ret {
76-
format!("return {}", snip)
77-
} else {
78-
snip.to_string()
79-
};
75+
if ret {
76+
snip = snip.make_return();
77+
}
8078

8179
if parent_node_is_if_expr(&e, &cx) {
82-
hint = format!("{{ {} }}", hint);
80+
snip = snip.blockify()
8381
}
8482

8583
span_lint_and_sugg(
@@ -88,7 +86,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
8886
e.span,
8987
"this if-then-else expression returns a bool literal",
9088
"you can reduce it to",
91-
hint,
89+
snip.to_string(),
9290
applicability,
9391
);
9492
};

clippy_lints/src/panic_unimplemented.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use rustc::hir::*;
44
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
55
use rustc::{declare_tool_lint, lint_array};
66
use syntax::ast::LitKind;
7-
use syntax::ext::quote::rt::Span;
87
use syntax::ptr::P;
8+
use syntax_pos::Span;
99

1010
/// **What it does:** Checks for missing parameters in `panic!`.
1111
///

clippy_lints/src/use_self.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,15 @@ impl LintPass for UseSelf {
5656
const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
5757

5858
fn span_use_self_lint(cx: &LateContext<'_, '_>, path: &Path) {
59+
// path segments only include actual path, no methods or fields
60+
let last_path_span = path.segments.last().expect(SEGMENTS_MSG).ident.span;
61+
// only take path up to the end of last_path_span
62+
let span = path.span.with_hi(last_path_span.hi());
63+
5964
span_lint_and_sugg(
6065
cx,
6166
USE_SELF,
62-
path.span,
67+
span,
6368
"unnecessary structure name repetition",
6469
"use the applicable keyword",
6570
"Self".to_owned(),

clippy_lints/src/utils/sugg.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,17 @@ impl<'a> Sugg<'a> {
206206
make_unop("&mut *", self)
207207
}
208208

209+
/// Convenience method to transform suggestion into a return call
210+
pub fn make_return(self) -> Sugg<'static> {
211+
Sugg::NonParen(Cow::Owned(format!("return {}", self)))
212+
}
213+
214+
/// Convenience method to transform suggestion into a block
215+
/// where the suggestion is a trailing expression
216+
pub fn blockify(self) -> Sugg<'static> {
217+
Sugg::NonParen(Cow::Owned(format!("{{ {} }}", self)))
218+
}
219+
209220
/// Convenience method to create the `<lhs>..<rhs>` or `<lhs>...<rhs>`
210221
/// suggestion.
211222
#[allow(dead_code)]
@@ -578,3 +589,21 @@ impl<'a, 'b, 'c, T: LintContext<'c>> DiagnosticBuilderExt<'c, T> for rustc_error
578589
self.span_suggestion_with_applicability(remove_span, msg, String::new(), applicability);
579590
}
580591
}
592+
593+
#[cfg(test)]
594+
mod test {
595+
use super::Sugg;
596+
use std::borrow::Cow;
597+
598+
const SUGGESTION: Sugg<'static> = Sugg::NonParen(Cow::Borrowed("function_call()"));
599+
600+
#[test]
601+
fn make_return_transform_sugg_into_a_return_call() {
602+
assert_eq!("return function_call()", SUGGESTION.make_return().to_string());
603+
}
604+
605+
#[test]
606+
fn blockify_transforms_sugg_into_a_block() {
607+
assert_eq!("{ function_call() }", SUGGESTION.blockify().to_string());
608+
}
609+
}

tests/missing-test-files.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::assertions_on_constants)]
2+
13
use std::fs::{self, DirEntry};
24
use std::path::Path;
35

tests/ui/assertions_on_constants.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn main() {
2+
assert!(true);
3+
assert!(false);
4+
assert!(true, "true message");
5+
assert!(false, "false message");
6+
7+
const B: bool = true;
8+
assert!(B);
9+
10+
const C: bool = false;
11+
assert!(C);
12+
}

0 commit comments

Comments
 (0)