Skip to content

Commit a239b22

Browse files
committed
start excessive_indentation
1 parent acb85c3 commit a239b22

File tree

10 files changed

+328
-124
lines changed

10 files changed

+328
-124
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/target-ra
2+
13
# Used by CI to be able to push:
24
/.github/deploy_key
35
out

clippy_lints/src/declared_lints.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
154154
crate::eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS_INFO,
155155
crate::excessive_bools::FN_PARAMS_EXCESSIVE_BOOLS_INFO,
156156
crate::excessive_bools::STRUCT_EXCESSIVE_BOOLS_INFO,
157-
crate::excessive_width::EXCESSIVE_INDENTATION_INFO,
158-
crate::excessive_width::EXCESSIVE_WIDTH_INFO,
157+
crate::excessive_indentation::EXCESSIVE_INDENTATION_INFO,
158+
crate::excessive_indentation::EXCESSIVE_WIDTH_INFO,
159159
crate::exhaustive_items::EXHAUSTIVE_ENUMS_INFO,
160160
crate::exhaustive_items::EXHAUSTIVE_STRUCTS_INFO,
161161
crate::exit::EXIT_INFO,
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use rustc_ast::{
3+
ast::{ItemKind, UseTree, UseTreeKind},
4+
Item,
5+
};
6+
use rustc_hir::*;
7+
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
8+
use rustc_middle::lint::in_external_macro;
9+
use rustc_session::{declare_tool_lint, impl_lint_pass};
10+
use rustc_span::CharPos;
11+
use rustc_span::Pos;
12+
use rustc_span::Span;
13+
14+
// TODO: This still needs to be implemented.
15+
declare_clippy_lint! {
16+
/// ### What it does
17+
///
18+
/// Checks for lines which are indented beyond a certain threshold.
19+
///
20+
/// ### Why is this bad?
21+
///
22+
/// It can severely hinder readability. The default is very generous; if you
23+
/// exceed this, it's a sign you should refactor.
24+
///
25+
/// ### Example
26+
/// TODO
27+
/// Use instead:
28+
/// TODO
29+
#[clippy::version = "1.70.0"]
30+
pub EXCESSIVE_INDENTATION,
31+
nursery,
32+
"checks for lines indented beyond a certain threshold"
33+
}
34+
declare_clippy_lint! {
35+
/// ### What it does
36+
///
37+
/// Checks for lines which are longer than a certain threshold.
38+
///
39+
/// ### Why is this bad?
40+
///
41+
/// It can severely hinder readability. Running rustfmt will almost always get any
42+
/// lines below this threshold (or whatever you have set as max_width), but if it
43+
/// fails, it's probably a sign you should refactor.
44+
///
45+
/// ### Example
46+
/// TODO
47+
/// Use instead:
48+
/// TODO
49+
#[clippy::version = "1.70.0"]
50+
pub EXCESSIVE_WIDTH,
51+
nursery,
52+
"checks for lines longer than a certain threshold"
53+
}
54+
// TODO: Split this into 2 structs
55+
impl_lint_pass!(ExcessiveIndentation => [EXCESSIVE_INDENTATION, EXCESSIVE_WIDTH]);
56+
57+
#[derive(Clone, Copy)]
58+
pub struct ExcessiveIndentation {
59+
pub excessive_width_threshold: u64,
60+
pub excessive_width_ignore_indentation: bool,
61+
pub excessive_indentation_threshold: u64,
62+
}
63+
64+
impl EarlyLintPass for ExcessiveIndentation {
65+
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
66+
match &item.kind {
67+
// TODO:
68+
// Use ✅
69+
// Static ❌
70+
// Const ❌
71+
// Fn ❌
72+
// Mod ❌
73+
// ForeignMod ❌
74+
// Enum ❌
75+
// Struct ❌
76+
// Union ❌
77+
// Trait ❌
78+
// Impl ❌
79+
// MacroDef ❓
80+
// Others don't increase indentation (unless your formatting is really weird) so they won't be included.
81+
ItemKind::Use(use_tree) => walk_use_tree(self, cx, use_tree, 0),
82+
_ => (),
83+
}
84+
}
85+
}
86+
87+
fn check_indent(conf: &mut ExcessiveIndentation, cx: &EarlyContext<'_>, span: Span, indent: u64) -> bool {
88+
if indent > conf.excessive_indentation_threshold {
89+
span_lint_and_help(cx, EXCESSIVE_INDENTATION, span, "a", None, "b");
90+
91+
return true;
92+
}
93+
94+
false
95+
}
96+
97+
fn walk_use_tree(conf: &mut ExcessiveIndentation, cx: &EarlyContext<'_>, use_tree: &UseTree, indent: u64) {
98+
if check_indent(conf, cx, use_tree.span, indent) {
99+
return;
100+
}
101+
102+
if let UseTreeKind::Nested(use_trees) = use_tree.kind.clone() {
103+
for use_tree in use_trees {
104+
walk_use_tree(conf, cx, &use_tree.0, indent + 1);
105+
}
106+
}
107+
}
108+
109+
impl LateLintPass<'_> for ExcessiveIndentation {
110+
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) {
111+
if in_external_macro(cx.sess(), stmt.span) {
112+
return;
113+
}
114+
115+
if let Ok(lines) = cx.sess().source_map().span_to_lines(stmt.span).map(|info| info.lines) {
116+
for line in &lines {
117+
let indentation = if self.excessive_width_ignore_indentation {
118+
line.start_col
119+
} else {
120+
CharPos::from_usize(0)
121+
};
122+
123+
// TODO: yeah, no.
124+
if (line.end_col.to_usize()
125+
- line.start_col.to_usize() * self.excessive_width_ignore_indentation as usize)
126+
> self.excessive_width_threshold as usize
127+
{
128+
span_lint_and_help(
129+
cx,
130+
EXCESSIVE_WIDTH,
131+
stmt.span,
132+
"this line is too long",
133+
None,
134+
"consider running rustfmt or refactoring this",
135+
);
136+
}
137+
}
138+
}
139+
}
140+
}

clippy_lints/src/excessive_width.rs

Lines changed: 0 additions & 82 deletions
This file was deleted.

clippy_lints/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ mod equatable_if_let;
120120
mod escape;
121121
mod eta_reduction;
122122
mod excessive_bools;
123-
mod excessive_width;
123+
mod excessive_indentation;
124124
mod exhaustive_items;
125125
mod exit;
126126
mod explicit_write;
@@ -966,8 +966,15 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
966966
let excessive_width_threshold = conf.excessive_width_threshold;
967967
let excessive_width_ignore_indentation = conf.excessive_width_ignore_indentation;
968968
let excessive_indentation_threshold = conf.excessive_indentation_threshold;
969+
store.register_early_pass(move || {
970+
Box::new(excessive_indentation::ExcessiveIndentation {
971+
excessive_width_threshold,
972+
excessive_width_ignore_indentation,
973+
excessive_indentation_threshold,
974+
})
975+
});
969976
store.register_late_pass(move |_| {
970-
Box::new(excessive_width::ExcessiveWidth {
977+
Box::new(excessive_indentation::ExcessiveIndentation {
971978
excessive_width_threshold,
972979
excessive_width_ignore_indentation,
973980
excessive_indentation_threshold,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![allow(unused)]
2+
#![warn(clippy::excessive_indentation)]
3+
4+
#[rustfmt::skip]
5+
fn main() {
6+
{
7+
{
8+
{
9+
{
10+
println!("warning! :)");
11+
}
12+
}
13+
}
14+
}
15+
}

tests/ui/excessive_indentation.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#![allow(unused)]
2+
#![warn(clippy::excessive_indentation)]
3+
4+
#[rustfmt::skip]
5+
use a::{b::{c::{d::{e::{f::{g::{h::{i::j::{k::{l::{m::{n::{o::{p::{}}}}}}}}}}}}}}};
6+
7+
pub mod a {
8+
pub mod b {
9+
pub mod c {
10+
pub mod d {
11+
pub mod e {
12+
pub mod f {
13+
pub mod g {
14+
pub mod h {
15+
pub mod i {
16+
pub mod j {
17+
pub mod k {
18+
pub mod l {
19+
pub mod m {
20+
pub mod n {
21+
pub mod o {
22+
pub mod p {}
23+
}
24+
}
25+
}
26+
}
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}
34+
}
35+
}
36+
}
37+
}
38+
39+
const A: u32 = 0u32;
40+
41+
#[rustfmt::skip]
42+
fn main() {
43+
{
44+
{
45+
{
46+
{
47+
{
48+
{
49+
{
50+
{
51+
{
52+
{
53+
{
54+
{
55+
{
56+
{
57+
let _x = {
58+
dbg!("hello");
59+
2
60+
};
61+
62+
println!("as you can tell, this is very excessive. you would never actually write this.")
63+
}
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}
72+
}
73+
}
74+
}
75+
}
76+
}
77+
}

tests/ui/excessive_indentation.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: a
2+
--> $DIR/excessive_indentation.rs:5:52
3+
|
4+
LL | use a::{b::{c::{d::{e::{f::{g::{h::{i::j::{k::{l::{m::{n::{o::{p::{}}}}}}}}}}}}}}};
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: b
8+
= note: `-D clippy::excessive-indentation` implied by `-D warnings`
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)