Skip to content

Commit 9668ae5

Browse files
authored
Rollup merge of #108726 - est31:backticks_matchmaking_tidy, r=Nilstrieb
tidy: enforce comment blocks to have an even number of backticks After PR #108694, most unmatched backticks in `compiler/` comments have been eliminated. This PR adds a tidy lint to ensure no new unmatched backticks are added, and either addresses the lint in the remaining instances it found, or allows it. Very often, backtick containing sections wrap around lines, for example: ```Rust // This function takes a tuple `(Vec<String>, // Box<[u8]>)` and transforms it into `Vec<u8>`. ``` The lint is implemented to work on top of blocks, counting each line with a `//` into a block, and counting if there are an odd or even number of backticks in the entire block, instead of looking at just a single line.
2 parents 501ad02 + b2aeb07 commit 9668ae5

File tree

12 files changed

+69
-21
lines changed

12 files changed

+69
-21
lines changed

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,7 @@ impl<'a> MethodDef<'a> {
10521052
/// ::core::hash::Hash::hash(&{ self.y }, state)
10531053
/// }
10541054
/// }
1055+
/// ```
10551056
fn expand_struct_method_body<'b>(
10561057
&self,
10571058
cx: &mut ExtCtxt<'_>,

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/native.rs

+1
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ fn build_enum_variant_member_di_node<'ll, 'tcx>(
438438
/// DW_TAG_structure_type (type of variant 1)
439439
/// DW_TAG_structure_type (type of variant 2)
440440
/// DW_TAG_structure_type (type of variant 3)
441+
/// ```
441442
struct VariantMemberInfo<'a, 'll> {
442443
variant_index: VariantIdx,
443444
variant_name: Cow<'a, str>,

compiler/rustc_error_codes/src/error_codes/E0368.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Add for Foo {
4141
4242
fn main() {
4343
let mut x: Foo = Foo(5);
44-
x += Foo(7); // error, `+= cannot be applied to the type `Foo`
44+
x += Foo(7); // error, `+=` cannot be applied to the type `Foo`
4545
}
4646
```
4747

compiler/rustc_error_codes/src/error_codes/E0710.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ An unknown tool name was found in a scoped lint.
33
Erroneous code examples:
44

55
```compile_fail,E0710
6-
#[allow(clipp::filter_map)] // error!`
6+
#[allow(clipp::filter_map)] // error!
77
fn main() {
88
// business logic
99
}
1010
```
1111

1212
```compile_fail,E0710
13-
#[warn(clipp::filter_map)] // error!`
13+
#[warn(clipp::filter_map)] // error!
1414
fn main() {
1515
// business logic
1616
}

compiler/rustc_hir_typeck/src/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
398398
///
399399
/// Here:
400400
/// - E would be `fn(&u32) -> &u32`.
401-
/// - S would be `fn(&u32) ->
401+
/// - S would be `fn(&u32) -> ?T`
402402
/// - E' is `&'!0 u32 -> &'!0 u32`
403403
/// - S' is `&'?0 u32 -> ?T`
404404
///

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
104104
let (mention_influencer, influencer_point) =
105105
if sup_origin.span().overlaps(param.param_ty_span) {
106106
// Account for `async fn` like in `async-await/issues/issue-62097.rs`.
107-
// The desugaring of `async `fn`s causes `sup_origin` and `param` to point at the same
107+
// The desugaring of `async fn`s causes `sup_origin` and `param` to point at the same
108108
// place (but with different `ctxt`, hence `overlaps` instead of `==` above).
109109
//
110110
// This avoids the following:

compiler/rustc_middle/src/mir/pretty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ fn dump_matched_mir_node<'tcx, F>(
123123
// see notes on #41697 above
124124
let def_path =
125125
ty::print::with_forced_impl_filename_line!(tcx.def_path_str(body.source.def_id()));
126+
// ignore-tidy-odd-backticks the literal below is fine
126127
write!(file, "// MIR for `{}", def_path)?;
127128
match body.source.promoted {
128129
None => write!(file, "`")?,

compiler/rustc_mir_build/src/build/matches/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
18861886
// let place = Foo::new();
18871887
// match place { Foo { .. } if { let tmp1 = &place; inspect(*tmp1) }
18881888
// => { let tmp2 = place; feed(tmp2) }, ... }
1889+
// ```
18891890
//
18901891
// And an input like:
18911892
//

compiler/rustc_trait_selection/src/solve/assembly.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
248248
///
249249
/// To deal with this, we first try to normalize the self type and add the candidates for the normalized
250250
/// self type to the list of candidates in case that succeeds. Note that we can't just eagerly return in
251-
/// this case as projections as self types add `
251+
/// this case as projections as self types add
252+
// FIXME complete the unfinished sentence above
252253
fn assemble_candidates_after_normalizing_self_ty<G: GoalKind<'tcx>>(
253254
&mut self,
254255
goal: Goal<'tcx, G>,

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2220,7 +2220,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
22202220
// - `BuiltinDerivedObligation` with a generator witness (A)
22212221
// - `BuiltinDerivedObligation` with a generator (A)
22222222
// - `BuiltinDerivedObligation` with `impl std::future::Future` (A)
2223-
// - `BindingObligation` with `impl_send (Send requirement)
2223+
// - `BindingObligation` with `impl_send` (Send requirement)
22242224
//
22252225
// The first obligation in the chain is the most useful and has the generator that captured
22262226
// the type. The last generator (`outer_generator` below) has information about where the

compiler/rustc_type_ir/src/fold.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! It defines a "skeleton" of how they should be folded.
1919
//! - `TypeSuperFoldable`. This is implemented only for each type of interest,
2020
//! and defines the folding "skeleton" for these types.
21-
//! - `TypeFolder`/`FallibleTypeFolder. One of these is implemented for each
21+
//! - `TypeFolder`/`FallibleTypeFolder`. One of these is implemented for each
2222
//! folder. This defines how types of interest are folded.
2323
//!
2424
//! This means each fold is a mixture of (a) generic folding operations, and (b)

src/tools/tidy/src/style.rs

+56-13
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ fn contains_ignore_directive(can_contain: bool, contents: &str, check: &str) ->
171171
}
172172

173173
macro_rules! suppressible_tidy_err {
174-
($err:ident, $skip:ident, $msg:expr) => {
174+
($err:ident, $skip:ident, $msg:literal) => {
175175
if let Directive::Deny = $skip {
176-
$err($msg);
176+
$err(&format!($msg));
177177
} else {
178178
$skip = Directive::Ignore(true);
179179
}
@@ -300,10 +300,13 @@ pub fn check(path: &Path, bad: &mut bool) {
300300
contains_ignore_directive(can_contain, &contents, "leading-newlines");
301301
let mut skip_copyright = contains_ignore_directive(can_contain, &contents, "copyright");
302302
let mut skip_dbg = contains_ignore_directive(can_contain, &contents, "dbg");
303+
let mut skip_odd_backticks =
304+
contains_ignore_directive(can_contain, &contents, "odd-backticks");
303305
let mut leading_new_lines = false;
304306
let mut trailing_new_lines = 0;
305307
let mut lines = 0;
306308
let mut last_safety_comment = false;
309+
let mut comment_block: Option<(usize, usize)> = None;
307310
let is_test = file.components().any(|c| c.as_os_str() == "tests");
308311
// scanning the whole file for multiple needles at once is more efficient than
309312
// executing lines times needles separate searches.
@@ -351,7 +354,7 @@ pub fn check(path: &Path, bad: &mut bool) {
351354
suppressible_tidy_err!(
352355
err,
353356
skip_line_length,
354-
&format!("line longer than {max_columns} chars")
357+
"line longer than {max_columns} chars"
355358
);
356359
}
357360
if !is_style_file && line.contains('\t') {
@@ -415,15 +418,55 @@ pub fn check(path: &Path, bad: &mut bool) {
415418

416419
// For now only enforce in compiler
417420
let is_compiler = || file.components().any(|c| c.as_os_str() == "compiler");
418-
if is_compiler()
419-
&& line.contains("//")
420-
&& line
421-
.chars()
422-
.collect::<Vec<_>>()
423-
.windows(4)
424-
.any(|cs| matches!(cs, ['.', ' ', ' ', last] if last.is_alphabetic()))
425-
{
426-
err(DOUBLE_SPACE_AFTER_DOT)
421+
422+
if is_compiler() {
423+
if line.contains("//")
424+
&& line
425+
.chars()
426+
.collect::<Vec<_>>()
427+
.windows(4)
428+
.any(|cs| matches!(cs, ['.', ' ', ' ', last] if last.is_alphabetic()))
429+
{
430+
err(DOUBLE_SPACE_AFTER_DOT)
431+
}
432+
433+
if filename.ends_with(".ftl") {
434+
let line_backticks = trimmed.chars().filter(|ch| *ch == '`').count();
435+
if line_backticks % 2 == 1 {
436+
suppressible_tidy_err!(err, skip_odd_backticks, "odd number of backticks");
437+
}
438+
} else if trimmed.contains("//") {
439+
let (start_line, mut backtick_count) = comment_block.unwrap_or((i + 1, 0));
440+
let line_backticks = trimmed.chars().filter(|ch| *ch == '`').count();
441+
let comment_text = trimmed.split("//").nth(1).unwrap();
442+
// This check ensures that we don't lint for code that has `//` in a string literal
443+
if line_backticks % 2 == 1 {
444+
backtick_count += comment_text.chars().filter(|ch| *ch == '`').count();
445+
}
446+
comment_block = Some((start_line, backtick_count));
447+
} else {
448+
if let Some((start_line, backtick_count)) = comment_block.take() {
449+
if backtick_count % 2 == 1 {
450+
let mut err = |msg: &str| {
451+
tidy_error!(bad, "{}:{start_line}: {msg}", file.display());
452+
};
453+
let block_len = (i + 1) - start_line;
454+
if block_len == 1 {
455+
suppressible_tidy_err!(
456+
err,
457+
skip_odd_backticks,
458+
"comment with odd number of backticks"
459+
);
460+
} else {
461+
suppressible_tidy_err!(
462+
err,
463+
skip_odd_backticks,
464+
"{block_len}-line comment block with odd number of backticks"
465+
);
466+
}
467+
}
468+
}
469+
}
427470
}
428471
}
429472
if leading_new_lines {
@@ -441,7 +484,7 @@ pub fn check(path: &Path, bad: &mut bool) {
441484
n => suppressible_tidy_err!(
442485
err,
443486
skip_trailing_newlines,
444-
&format!("too many trailing newlines ({n})")
487+
"too many trailing newlines ({n})"
445488
),
446489
};
447490
if lines > LINES {

0 commit comments

Comments
 (0)