Skip to content

Commit 97c1007

Browse files
committed
add the excessive_* style lints
1 parent 2360f80 commit 97c1007

File tree

10 files changed

+217
-0
lines changed

10 files changed

+217
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4758,7 +4758,9 @@ Released 2018-09-13
47584758
[`erasing_op`]: https://rust-lang.github.io/rust-clippy/master/index.html#erasing_op
47594759
[`err_expect`]: https://rust-lang.github.io/rust-clippy/master/index.html#err_expect
47604760
[`eval_order_dependence`]: https://rust-lang.github.io/rust-clippy/master/index.html#eval_order_dependence
4761+
[`excessive_indentation`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_indentation
47614762
[`excessive_precision`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_precision
4763+
[`excessive_width`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_width
47624764
[`exhaustive_enums`]: https://rust-lang.github.io/rust-clippy/master/index.html#exhaustive_enums
47634765
[`exhaustive_structs`]: https://rust-lang.github.io/rust-clippy/master/index.html#exhaustive_structs
47644766
[`exit`]: https://rust-lang.github.io/rust-clippy/master/index.html#exit

clippy_lints/src/declared_lints.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
159159
crate::eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS_INFO,
160160
crate::excessive_bools::FN_PARAMS_EXCESSIVE_BOOLS_INFO,
161161
crate::excessive_bools::STRUCT_EXCESSIVE_BOOLS_INFO,
162+
crate::excessive_width::EXCESSIVE_INDENTATION_INFO,
163+
crate::excessive_width::EXCESSIVE_WIDTH_INFO,
162164
crate::exhaustive_items::EXHAUSTIVE_ENUMS_INFO,
163165
crate::exhaustive_items::EXHAUSTIVE_STRUCTS_INFO,
164166
crate::exit::EXIT_INFO,

clippy_lints/src/excessive_width.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use rustc_hir::*;
3+
use rustc_lint::{LateContext, LateLintPass, LintContext};
4+
use rustc_middle::lint::in_external_macro;
5+
use rustc_session::{declare_tool_lint, impl_lint_pass};
6+
use rustc_span::Pos;
7+
8+
// TODO: This still needs to be implemented.
9+
declare_clippy_lint! {
10+
/// ### What it does
11+
///
12+
/// Checks for lines which are indented beyond a certain threshold.
13+
///
14+
/// ### Why is this bad?
15+
///
16+
/// It can severely hinder readability. The default is very generous; if you
17+
/// exceed this, it's a sign you should refactor.
18+
///
19+
/// ### Example
20+
/// TODO
21+
/// Use instead:
22+
/// TODO
23+
#[clippy::version = "1.70.0"]
24+
pub EXCESSIVE_INDENTATION,
25+
style,
26+
"check for lines intended beyond a certain threshold"
27+
}
28+
declare_clippy_lint! {
29+
/// ### What it does
30+
///
31+
/// Checks for lines which are longer than a certain threshold.
32+
///
33+
/// ### Why is this bad?
34+
///
35+
/// It can severely hinder readability. Almost always, running rustfmt will get this
36+
/// below this threshold (or whatever you have set as max_width), but if it fails,
37+
/// it's probably a sign you should refactor.
38+
///
39+
/// ### Example
40+
/// TODO
41+
/// Use instead:
42+
/// TODO
43+
#[clippy::version = "1.70.0"]
44+
pub EXCESSIVE_WIDTH,
45+
style,
46+
"check for lines longer than a certain threshold"
47+
}
48+
impl_lint_pass!(ExcessiveWidth => [EXCESSIVE_INDENTATION, EXCESSIVE_WIDTH]);
49+
50+
#[derive(Clone, Copy)]
51+
pub struct ExcessiveWidth {
52+
pub excessive_width_threshold: u64,
53+
pub excessive_width_ignore_indentation: bool,
54+
pub excessive_indentation_threshold: u64,
55+
}
56+
57+
impl LateLintPass<'_> for ExcessiveWidth {
58+
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) {
59+
if in_external_macro(cx.sess(), stmt.span) {
60+
return;
61+
}
62+
63+
if let Ok(lines) = cx.sess().source_map().span_to_lines(stmt.span).map(|info| info.lines) {
64+
for line in &lines {
65+
// TODO: yeah, no.
66+
if (line.end_col.to_usize()
67+
- line.start_col.to_usize() * self.excessive_width_ignore_indentation as usize)
68+
> self.excessive_width_threshold as usize
69+
{
70+
span_lint_and_help(
71+
cx,
72+
EXCESSIVE_WIDTH,
73+
stmt.span,
74+
"this line is too long",
75+
None,
76+
"consider running rustfmt or refactoring this",
77+
);
78+
}
79+
}
80+
}
81+
}
82+
}

clippy_lints/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ mod equatable_if_let;
122122
mod escape;
123123
mod eta_reduction;
124124
mod excessive_bools;
125+
mod excessive_width;
125126
mod exhaustive_items;
126127
mod exit;
127128
mod explicit_write;
@@ -1007,6 +1008,16 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10071008
store.register_late_pass(|_| Box::new(tests_outside_test_module::TestsOutsideTestModule));
10081009
store.register_late_pass(|_| Box::new(manual_slice_size_calculation::ManualSliceSizeCalculation));
10091010
store.register_early_pass(|| Box::new(suspicious_doc_comments::SuspiciousDocComments));
1011+
let excessive_width_threshold = conf.excessive_width_threshold;
1012+
let excessive_width_ignore_indentation = conf.excessive_width_ignore_indentation;
1013+
let excessive_indentation_threshold = conf.excessive_indentation_threshold;
1014+
store.register_late_pass(move |_| {
1015+
Box::new(excessive_width::ExcessiveWidth {
1016+
excessive_width_threshold,
1017+
excessive_width_ignore_indentation,
1018+
excessive_indentation_threshold,
1019+
})
1020+
});
10101021
store.register_late_pass(|_| Box::new(items_after_test_module::ItemsAfterTestModule));
10111022
store.register_early_pass(|| Box::new(ref_patterns::RefPatterns));
10121023
store.register_late_pass(|_| Box::new(default_constructed_unit_structs::DefaultConstructedUnitStructs));

clippy_lints/src/utils/conf.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,18 @@ define_Conf! {
305305
///
306306
/// The maximum cognitive complexity a function can have
307307
(cognitive_complexity_threshold: u64 = 25),
308+
/// Lint: EXCESSIVE_WIDTH.
309+
///
310+
/// The maximum width a statement can have
311+
(excessive_width_threshold: u64 = 100),
312+
/// Lint: EXCESSIVE_WIDTH.
313+
///
314+
/// Whether to ignore the line's indentation
315+
(excessive_width_ignore_indentation: bool = true),
316+
/// Lint: EXCESSIVE_INDENTATION.
317+
///
318+
/// The maximum indentation a statement can have
319+
(excessive_indentation_threshold: u64 = 10),
308320
/// DEPRECATED LINT: CYCLOMATIC_COMPLEXITY.
309321
///
310322
/// Use the Cognitive Complexity lint instead.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
excessive-width-threshold = 20
2+
excessive-width-ignore-indentation = false
3+
excessive-indentation-threshold = 3
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![allow(unused)]
2+
#![allow(clippy::identity_op)]
3+
#![allow(clippy::no_effect)]
4+
#![warn(clippy::excessive_width)]
5+
6+
static mut C: u32 = 2u32;
7+
8+
#[rustfmt::skip]
9+
fn main() {
10+
let x = 2 * unsafe { C };
11+
12+
{
13+
{
14+
// this too, even though it's only 15 characters!
15+
();
16+
}
17+
}
18+
19+
{
20+
{
21+
{
22+
println!("this will now emit a warning, how neat!")
23+
}
24+
}
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error: this line is too long
2+
--> $DIR/excessive_width.rs:10:5
3+
|
4+
LL | let x = 2 * unsafe { C };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: consider running rustfmt or refactoring this
8+
= note: `-D clippy::excessive-width` implied by `-D warnings`
9+
10+
error: this line is too long
11+
--> $DIR/excessive_width.rs:12:5
12+
|
13+
LL | / {
14+
LL | | {
15+
LL | | // this too, even though it's only 15 characters!
16+
LL | | ();
17+
LL | | }
18+
LL | | }
19+
| |_____^
20+
|
21+
= help: consider running rustfmt or refactoring this
22+
23+
error: aborting due to 2 previous errors
24+

tests/ui/excessive_width.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#![allow(unused)]
2+
#![allow(clippy::identity_op)]
3+
#![warn(clippy::excessive_width)]
4+
5+
#[rustfmt::skip]
6+
fn main() {
7+
let x = 1;
8+
9+
let really_long_binding_name_because_this_needs_to_be_over_90_characters_long = 1usize * 200 / 2 * 500 / 1;
10+
11+
{
12+
{
13+
{
14+
{
15+
{
16+
{
17+
{
18+
{
19+
{
20+
{
21+
{
22+
{
23+
{
24+
{
25+
{
26+
{
27+
println!("highly indented lines do not cause a warning (by default)!")
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}

tests/ui/excessive_width.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: this line is too long
2+
--> $DIR/excessive_width.rs:9:5
3+
|
4+
LL | let really_long_binding_name_because_this_needs_to_be_over_90_characters_long = 1usize * 200 / 2 * 500 / 1;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: consider running rustfmt or refactoring this
8+
= note: `-D clippy::excessive-width` implied by `-D warnings`
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)