diff options
| -rw-r--r-- | CHANGELOG.md | 2 | ||||
| -rw-r--r-- | clippy_lints/src/declared_lints.rs | 2 | ||||
| -rw-r--r-- | clippy_lints/src/excessive_width.rs | 82 | ||||
| -rw-r--r-- | clippy_lints/src/lib.rs | 11 | ||||
| -rw-r--r-- | clippy_lints/src/utils/conf.rs | 12 | ||||
| -rw-r--r-- | tests/ui-toml/excessive_width/clippy.toml | 3 | ||||
| -rw-r--r-- | tests/ui-toml/excessive_width/excessive_width.rs | 26 | ||||
| -rw-r--r-- | tests/ui-toml/excessive_width/excessive_width.stderr | 24 | ||||
| -rw-r--r-- | tests/ui/excessive_width.rs | 44 | ||||
| -rw-r--r-- | tests/ui/excessive_width.stderr | 11 |
10 files changed, 217 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index a65ac4d46de..76fd81a25c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4758,7 +4758,9 @@ Released 2018-09-13 [`erasing_op`]: https://rust-lang.github.io/rust-clippy/master/index.html#erasing_op [`err_expect`]: https://rust-lang.github.io/rust-clippy/master/index.html#err_expect [`eval_order_dependence`]: https://rust-lang.github.io/rust-clippy/master/index.html#eval_order_dependence +[`excessive_indentation`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_indentation [`excessive_precision`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_precision +[`excessive_width`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_width [`exhaustive_enums`]: https://rust-lang.github.io/rust-clippy/master/index.html#exhaustive_enums [`exhaustive_structs`]: https://rust-lang.github.io/rust-clippy/master/index.html#exhaustive_structs [`exit`]: https://rust-lang.github.io/rust-clippy/master/index.html#exit diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index d014534cee9..20911348690 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -159,6 +159,8 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS_INFO, crate::excessive_bools::FN_PARAMS_EXCESSIVE_BOOLS_INFO, crate::excessive_bools::STRUCT_EXCESSIVE_BOOLS_INFO, + crate::excessive_width::EXCESSIVE_INDENTATION_INFO, + crate::excessive_width::EXCESSIVE_WIDTH_INFO, crate::exhaustive_items::EXHAUSTIVE_ENUMS_INFO, crate::exhaustive_items::EXHAUSTIVE_STRUCTS_INFO, crate::exit::EXIT_INFO, diff --git a/clippy_lints/src/excessive_width.rs b/clippy_lints/src/excessive_width.rs new file mode 100644 index 00000000000..84ae54f401e --- /dev/null +++ b/clippy_lints/src/excessive_width.rs @@ -0,0 +1,82 @@ +use clippy_utils::diagnostics::span_lint_and_help; +use rustc_hir::*; +use rustc_lint::{LateContext, LateLintPass, LintContext}; +use rustc_middle::lint::in_external_macro; +use rustc_session::{declare_tool_lint, impl_lint_pass}; +use rustc_span::Pos; + +// TODO: This still needs to be implemented. +declare_clippy_lint! { + /// ### What it does + /// + /// Checks for lines which are indented beyond a certain threshold. + /// + /// ### Why is this bad? + /// + /// It can severely hinder readability. The default is very generous; if you + /// exceed this, it's a sign you should refactor. + /// + /// ### Example + /// TODO + /// Use instead: + /// TODO + #[clippy::version = "1.70.0"] + pub EXCESSIVE_INDENTATION, + style, + "check for lines intended beyond a certain threshold" +} +declare_clippy_lint! { + /// ### What it does + /// + /// Checks for lines which are longer than a certain threshold. + /// + /// ### Why is this bad? + /// + /// It can severely hinder readability. Almost always, running rustfmt will get this + /// below this threshold (or whatever you have set as max_width), but if it fails, + /// it's probably a sign you should refactor. + /// + /// ### Example + /// TODO + /// Use instead: + /// TODO + #[clippy::version = "1.70.0"] + pub EXCESSIVE_WIDTH, + style, + "check for lines longer than a certain threshold" +} +impl_lint_pass!(ExcessiveWidth => [EXCESSIVE_INDENTATION, EXCESSIVE_WIDTH]); + +#[derive(Clone, Copy)] +pub struct ExcessiveWidth { + pub excessive_width_threshold: u64, + pub excessive_width_ignore_indentation: bool, + pub excessive_indentation_threshold: u64, +} + +impl LateLintPass<'_> for ExcessiveWidth { + fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { + if in_external_macro(cx.sess(), stmt.span) { + return; + } + + if let Ok(lines) = cx.sess().source_map().span_to_lines(stmt.span).map(|info| info.lines) { + for line in &lines { + // TODO: yeah, no. + if (line.end_col.to_usize() + - line.start_col.to_usize() * self.excessive_width_ignore_indentation as usize) + > self.excessive_width_threshold as usize + { + span_lint_and_help( + cx, + EXCESSIVE_WIDTH, + stmt.span, + "this line is too long", + None, + "consider running rustfmt or refactoring this", + ); + } + } + } + } +} diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index d53c6de5451..8b08861bc1d 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -122,6 +122,7 @@ mod equatable_if_let; mod escape; mod eta_reduction; mod excessive_bools; +mod excessive_width; mod exhaustive_items; mod exit; mod explicit_write; @@ -1007,6 +1008,16 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(tests_outside_test_module::TestsOutsideTestModule)); store.register_late_pass(|_| Box::new(manual_slice_size_calculation::ManualSliceSizeCalculation)); store.register_early_pass(|| Box::new(suspicious_doc_comments::SuspiciousDocComments)); + let excessive_width_threshold = conf.excessive_width_threshold; + let excessive_width_ignore_indentation = conf.excessive_width_ignore_indentation; + let excessive_indentation_threshold = conf.excessive_indentation_threshold; + store.register_late_pass(move |_| { + Box::new(excessive_width::ExcessiveWidth { + excessive_width_threshold, + excessive_width_ignore_indentation, + excessive_indentation_threshold, + }) + }); store.register_late_pass(|_| Box::new(items_after_test_module::ItemsAfterTestModule)); store.register_early_pass(|| Box::new(ref_patterns::RefPatterns)); store.register_late_pass(|_| Box::new(default_constructed_unit_structs::DefaultConstructedUnitStructs)); diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 35f40830681..1bdb5e42418 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -305,6 +305,18 @@ define_Conf! { /// /// The maximum cognitive complexity a function can have (cognitive_complexity_threshold: u64 = 25), + /// Lint: EXCESSIVE_WIDTH. + /// + /// The maximum width a statement can have + (excessive_width_threshold: u64 = 100), + /// Lint: EXCESSIVE_WIDTH. + /// + /// Whether to ignore the line's indentation + (excessive_width_ignore_indentation: bool = true), + /// Lint: EXCESSIVE_INDENTATION. + /// + /// The maximum indentation a statement can have + (excessive_indentation_threshold: u64 = 10), /// DEPRECATED LINT: CYCLOMATIC_COMPLEXITY. /// /// Use the Cognitive Complexity lint instead. diff --git a/tests/ui-toml/excessive_width/clippy.toml b/tests/ui-toml/excessive_width/clippy.toml new file mode 100644 index 00000000000..1824c8a3127 --- /dev/null +++ b/tests/ui-toml/excessive_width/clippy.toml @@ -0,0 +1,3 @@ +excessive-width-threshold = 20 +excessive-width-ignore-indentation = false +excessive-indentation-threshold = 3 diff --git a/tests/ui-toml/excessive_width/excessive_width.rs b/tests/ui-toml/excessive_width/excessive_width.rs new file mode 100644 index 00000000000..261b937e72e --- /dev/null +++ b/tests/ui-toml/excessive_width/excessive_width.rs @@ -0,0 +1,26 @@ +#![allow(unused)] +#![allow(clippy::identity_op)] +#![allow(clippy::no_effect)] +#![warn(clippy::excessive_width)] + +static mut C: u32 = 2u32; + +#[rustfmt::skip] +fn main() { + let x = 2 * unsafe { C }; + + { + { + // this too, even though it's only 15 characters! + (); + } + } + + { + { + { + println!("this will now emit a warning, how neat!") + } + } + } +} diff --git a/tests/ui-toml/excessive_width/excessive_width.stderr b/tests/ui-toml/excessive_width/excessive_width.stderr new file mode 100644 index 00000000000..00dce391be0 --- /dev/null +++ b/tests/ui-toml/excessive_width/excessive_width.stderr @@ -0,0 +1,24 @@ +error: this line is too long + --> $DIR/excessive_width.rs:10:5 + | +LL | let x = 2 * unsafe { C }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: consider running rustfmt or refactoring this + = note: `-D clippy::excessive-width` implied by `-D warnings` + +error: this line is too long + --> $DIR/excessive_width.rs:12:5 + | +LL | / { +LL | | { +LL | | // this too, even though it's only 15 characters! +LL | | (); +LL | | } +LL | | } + | |_____^ + | + = help: consider running rustfmt or refactoring this + +error: aborting due to 2 previous errors + diff --git a/tests/ui/excessive_width.rs b/tests/ui/excessive_width.rs new file mode 100644 index 00000000000..218950f9684 --- /dev/null +++ b/tests/ui/excessive_width.rs @@ -0,0 +1,44 @@ +#![allow(unused)] +#![allow(clippy::identity_op)] +#![warn(clippy::excessive_width)] + +#[rustfmt::skip] +fn main() { + let x = 1; + + let really_long_binding_name_because_this_needs_to_be_over_90_characters_long = 1usize * 200 / 2 * 500 / 1; + + { + { + { + { + { + { + { + { + { + { + { + { + { + { + { + { + println!("highly indented lines do not cause a warning (by default)!") + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } +} diff --git a/tests/ui/excessive_width.stderr b/tests/ui/excessive_width.stderr new file mode 100644 index 00000000000..707a3796b56 --- /dev/null +++ b/tests/ui/excessive_width.stderr @@ -0,0 +1,11 @@ +error: this line is too long + --> $DIR/excessive_width.rs:9:5 + | +LL | let really_long_binding_name_because_this_needs_to_be_over_90_characters_long = 1usize * 200 / 2 * 500 / 1; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: consider running rustfmt or refactoring this + = note: `-D clippy::excessive-width` implied by `-D warnings` + +error: aborting due to previous error + |
