add the excessive_*
style lints
This commit is contained in:
parent
2360f80143
commit
97c10075ec
@ -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
|
||||
|
@ -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,
|
||||
|
82
clippy_lints/src/excessive_width.rs
Normal file
82
clippy_lints/src/excessive_width.rs
Normal file
@ -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",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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));
|
||||
|
@ -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.
|
||||
|
3
tests/ui-toml/excessive_width/clippy.toml
Normal file
3
tests/ui-toml/excessive_width/clippy.toml
Normal file
@ -0,0 +1,3 @@
|
||||
excessive-width-threshold = 20
|
||||
excessive-width-ignore-indentation = false
|
||||
excessive-indentation-threshold = 3
|
26
tests/ui-toml/excessive_width/excessive_width.rs
Normal file
26
tests/ui-toml/excessive_width/excessive_width.rs
Normal file
@ -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!")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
24
tests/ui-toml/excessive_width/excessive_width.stderr
Normal file
24
tests/ui-toml/excessive_width/excessive_width.stderr
Normal file
@ -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
|
||||
|
44
tests/ui/excessive_width.rs
Normal file
44
tests/ui/excessive_width.rs
Normal file
@ -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)!")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
tests/ui/excessive_width.stderr
Normal file
11
tests/ui/excessive_width.stderr
Normal file
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user