rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.51k stars 1.55k forks source link

tabs_in_doc_comments: Clippy flags tab-indented code in doc tests #12425

Open printfn opened 9 months ago

printfn commented 9 months ago

Summary

The tabs_in_doc_comments complains about tab-indented doctests.

Running cargo doc generates nice-looking and correctly-indented documentation when using tabs in this way, so I see no reason for Clippy to emit a lint.

Note that rustfmt doesn't change the formatting/indentation of doctests like this regardless of whether hard_tabs = true is set in .rustfmt.toml.

Lint Name

tabs_in_doc_comments

Reproducer

I tried this code:

/// ```
/// fn testing() {
///     println!("this line is tab-indented")
/// }
/// ```
pub fn foo() {
    println!("Hello, world")
}

I saw this happen:

    Checking clippy-repro v1.4.4 (/home/user/fend/clippy-repro)
warning: using tabs in doc comments is not recommended
 --> clippy-repro/src/lib.rs:3:5
  |
3 | ///     println!("this line is tab-indented")
  |     ^^^^ help: consider using four spaces per tab
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
  = note: `#[warn(clippy::tabs_in_doc_comments)]` on by default

warning: `clippy-repro` (lib) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s

I expected to see this happen: Clippy should not have linted that line.

Version

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6

Additional Labels

No response

J-ZhengLi commented 8 months ago

@rustbot claim

Qix- commented 5 months ago

Note that rustfmt doesn't change the formatting/indentation of doctests like this regardless of whether hard_tabs = true is set in .rustfmt.toml.

I noticed this as well. After this lands, should an issue be opened on the rustfmt repository as well to handle this case? Would be really nice!

cdellacqua commented 2 weeks ago

If I may add to the original issue, tabs as an indentation mechanism wouldn't break ascii-diagrams as vaguely implied by the wording in the "Why is this bad?" section of the lint documentation. They're only a problem when used as an alignment mechanism and, of course, it would be perfectly fine for Clippy to report this kind of usage.

For example, none of the following examples have issues, independently of the tab width preference configured by the user:

///<space>if n > 42 {
///<space><tab>println!("correctly aligned by the space and indented by the tab");
///<space>}

or even

/// <space><tab><space>test 1< 4 spaces>one
/// <space><tab><space>test 10<3 spaces>two