mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] checkpatch.pl: add warning for // comments on private Rust items
@ 2025-04-19 20:24 Arnaud Lecomte
  2025-04-19 21:24 ` [PATCH 1/2] checkpatch.pl: warn about " Arnaud Lecomte
  2025-04-20  8:15 ` [PATCH 0/2] checkpatch.pl: add warning for // " Benno Lossin
  0 siblings, 2 replies; 5+ messages in thread
From: Arnaud Lecomte @ 2025-04-19 20:24 UTC (permalink / raw)
  To: Andy Whitcroft, Joe Perches, Dwaipayan Ray, Lukas Bulwahn,
	Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Nathan Chancellor,
	Nick Desaulniers, Bill Wendling, Justin Stitt
  Cc: linux-kernel, rust-for-linux, llvm, contact, skhan

Hi,

Background
----------

The Rust-for-Linux project currently lacks enforcement of documentation for private Rust items, 
as highlighted in https://github.com/Rust-for-Linux/linux/issues/1157.
While rustc already lints missing documentation for public items, private items remain unchecked.
This patch series aims to close that gap by ensuring proper documentation practices
for private Rust items in the kernel.
As underlined in this issue:
https://github.com/Rust-for-Linux/linux/issues/1157, the purposes of
this patch serie is to ensure the proper documentation of private rust
items. Public items missing documentation are already linted by rustc. 

The actual solution comes in several parts
------------------------------------------

 1) Patch 1 : Implements detection logic to emit warnings for improperly
 documented private Rust items (e.g., // comments instead of ///).
 2) Patch 2 : Adds an auto-fix mechanism via the --fix option to help
 developers correct documentation issues.

Results
--------------------

The following implementation has been tested against this input file:
// SPDX-License-Identifier: GPL-2.0

// Simple comment - should not trigger

// Returns a reference to the underlying [`cpufreq::Table`]. - should trigger

fn table(&self) -> &cpufreq::Table {
    // SAFETY: The `ptr` is guaranteed by the C code to be valid. - should not trigger
    unsafe { cpufreq::Table::from_raw(self.ptr) }
}

// Improper doc comment for a private function. - should trigger
fn test() -> u32 {
    42
}

/// Proper doc comment for a private function. - should not trigger
fn proper_doc() -> u32 {
    42
}

// TODO: implement more logic - should not trigger
fn todo_marker() -> bool {
    true
}

// Just a regular comment not followed by code - should not trigger

pub fn public_function() {
    // Public function - should not trigger
}

// Test - should trigger

struct Point2D {
  pub x: f32,
  pub y: f32
}

// Test - should not trigger

pub struct Point2D {
  pub x: f32,
  pub y: f32
}

// Test - should not trigger

pub struct Point2D {
  pub x: f32,
  pub y: f32
}

mod test_module {
    // Module inner comment - should not trigger
}

// Comment before macro - should not trigger
macro_rules! my_macro {
    // Comment inside macro - should not trigger
    () => {};
}

// Comment before unsafe block - should not trigger
unsafe {
    // Comment inside unsafe block - should not trigger
    let x = 5;
}

// Comment with unsafe word - should trigger
fn with_unsafe_keyword() {
    println!("test");
}

// Comment with code example: - should trigger
// let x = 5; - should trigger
fn with_code_example() {
    println!("test");
}

// NOTE: important consideration - should not trigger
fn note_marker() -> bool {
    true
}

// Comment with code example: - should trigger
/// let x = 5; - should not trigger
fn with_mixed_comments() {
    println!("test");
}

which led to this output:
WARNING: Consider using `///` for private item documentation (line 5)

+// Returns a reference to the underlying [`cpufreq::Table`]. - should trigger
WARNING: Consider using `///` for private item documentation (line 12)

+// Improper doc comment for a private function. - should trigger
WARNING: Consider using `///` for private item documentation (line 33)

+// Test - should trigger
WARNING: Consider using `///` for private item documentation (line 74)

+// Comment with unsafe word - should trigger
WARNING: Consider using `///` for private item documentation (line 79)

+// Comment with code example: - should trigger
WARNING: Consider using `///` for private item documentation (line 80)

+// let x = 5; - should trigger
WARNING: Consider using `///` for private item documentation (line 91)

+// Comment with code example: - should trigger
total: 0 errors, 7 warnings, 95 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

test.rs has style problems, please review.

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.

Link: https://github.com/Rust-for-Linux/linux/issues/1157
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
---
Arnaud Lecomte (2):
      checkpatch.pl: warn about // comments on private Rust items
      checkpatch.pl: add `--fix` support for `//` comments on private Rust items

 scripts/checkpatch.pl | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
---
base-commit: 250b341caf5e0e099dcf789be652178ca57a68cd
change-id: 20250419-checkpatch-rust-private-item-comment-bb3a5281e5da

Best regards,
-- 
Arnaud Lecomte <contact@arnaud-lcm.com>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] checkpatch.pl: warn about // comments on private Rust items
  2025-04-19 20:24 [PATCH 0/2] checkpatch.pl: add warning for // comments on private Rust items Arnaud Lecomte
@ 2025-04-19 21:24 ` Arnaud Lecomte
  2025-04-19 22:25   ` [PATCH 2/2] checkpatch.pl: add `--fix` support for `//` " Arnaud Lecomte
  2025-04-20  8:15 ` [PATCH 0/2] checkpatch.pl: add warning for // " Benno Lossin
  1 sibling, 1 reply; 5+ messages in thread
From: Arnaud Lecomte @ 2025-04-19 21:24 UTC (permalink / raw)
  To: Andy Whitcroft, Joe Perches, Dwaipayan Ray, Lukas Bulwahn,
	Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Nathan Chancellor,
	Nick Desaulniers, Bill Wendling, Justin Stitt
  Cc: linux-kernel, rust-for-linux, llvm, contact, skhan

This patch adds a checkpatch.pl warning to detect //-style comments
placed above private Rust items.

Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
---
 scripts/checkpatch.pl | 49 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 3d22bf863eec..7ef658a70bfc 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1987,6 +1987,34 @@ sub ctx_locate_comment {
 	chomp($current_comment);
 	return($current_comment);
 }
+
+sub ctx_get_comment_behind_rust_private_item {
+    my ($linenr) = @_;
+    my $start_line = $linenr - 1;
+    my $idx = $start_line;
+
+    # Skip if it's a public Rust item (starts with 'pub')
+    return unless $rawlines[$start_line] !~ /^\+?\s*pub(\s|\([^)]*\)\s)/;
+    
+    # Check if it's a Rust item
+    return unless $rawlines[$start_line] =~ /^\+?\s*\b(fn|struct|const|enum|trait|type|mod|impl|use)\b/;
+
+    # Check if it's an empty line
+    return unless $rawlines[$start_line] !~ /^\s*\+?\s*$/;
+
+    # Check if it's a comment
+    return unless $rawlines[$start_line] !~ /^\+?\s*\/\/\/?\/?.*$/;
+
+    # Walk backwards through non-empty lines
+    $idx--;
+    while ($idx >= 0 && $rawlines[$idx] !~ /^\s*\+?\s*$/) {
+        $idx--;
+    }
+
+    return ($idx+1, $start_line);
+}
+
+
 sub ctx_has_comment {
 	my ($first_line, $end_line) = @_;
 	my $cmt = ctx_locate_comment($first_line, $end_line);
@@ -2950,6 +2978,27 @@ sub process {
 			}
 		}
 
+# check for incorrect use of `//` instead of `\\\` for doc comments in Rust
+		if ($perl_version_ok && $realfile =~ /\.rs$/) {
+			my ($start_l, $end_l) = ctx_get_comment_behind_rust_private_item($linenr);
+			if (defined($start_l) && defined($end_l)) {
+				for (my $i = $start_l; $i <= $end_l; $i++) {
+					my $comment_line = $rawlines[$i - 1];
+					next unless $comment_line =~ m@^\+//([^/].*)$@;
+					my $comment = $1;
+			
+					# Skip obvious non-doc comments
+					next if $comment =~ m@^\s*(?:TODO|FIXME|XXX|NOTE|HACK|SAFETY):?@i;
+					next if $comment =~ m@^\s*[[:punct:]]{2,}@;	
+					my $line_issue = $i - 3;
+					
+					WARN("POSSIBLE_MISSING_RUST_DOC",
+						"Consider using `///` for private item documentation (line $line_issue)\n" .
+					"$here\n$comment_line");
+				}
+			}
+		}
+
 # Check the patch for a From:
 		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
 			$author = $1;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/2] checkpatch.pl: add `--fix` support for `//` comments on private Rust items
  2025-04-19 21:24 ` [PATCH 1/2] checkpatch.pl: warn about " Arnaud Lecomte
@ 2025-04-19 22:25   ` Arnaud Lecomte
  0 siblings, 0 replies; 5+ messages in thread
From: Arnaud Lecomte @ 2025-04-19 22:25 UTC (permalink / raw)
  To: Andy Whitcroft, Joe Perches, Dwaipayan Ray, Lukas Bulwahn,
	Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Nathan Chancellor,
	Nick Desaulniers, Bill Wendling, Justin Stitt
  Cc: linux-kernel, rust-for-linux, llvm, contact, skhan

Extend the Rust private item documentation checker (added in the previous
patch) to support automatic fixes when the `--fix` option is enabled.

Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
---
 scripts/checkpatch.pl | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 7ef658a70bfc..ce3ba9af6da7 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -2992,9 +2992,11 @@ sub process {
 					next if $comment =~ m@^\s*[[:punct:]]{2,}@;	
 					my $line_issue = $i - 3;
 					
-					WARN("POSSIBLE_MISSING_RUST_DOC",
+					if(WARN("POSSIBLE_MISSING_RUST_DOC",
 						"Consider using `///` for private item documentation (line $line_issue)\n" .
-					"$here\n$comment_line");
+					"$here\n$comment_line") && $fix) {
+						$fixed[$i - 1] =~ s/^\+(\/\/)/+$1\//;	
+					}
 				}
 			}
 		}
-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 0/2] checkpatch.pl: add warning for // comments on private Rust items
  2025-04-19 20:24 [PATCH 0/2] checkpatch.pl: add warning for // comments on private Rust items Arnaud Lecomte
  2025-04-19 21:24 ` [PATCH 1/2] checkpatch.pl: warn about " Arnaud Lecomte
@ 2025-04-20  8:15 ` Benno Lossin
  1 sibling, 0 replies; 5+ messages in thread
From: Benno Lossin @ 2025-04-20  8:15 UTC (permalink / raw)
  To: Arnaud Lecomte, Andy Whitcroft, Joe Perches, Dwaipayan Ray,
	Lukas Bulwahn, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Nathan Chancellor, Nick Desaulniers,
	Bill Wendling, Justin Stitt
  Cc: linux-kernel, rust-for-linux, llvm, skhan

On Sat Apr 19, 2025 at 10:24 PM CEST, Arnaud Lecomte wrote:
> Hi,
>
> Background
> ----------
>
> The Rust-for-Linux project currently lacks enforcement of documentation for private Rust items,
> as highlighted in https://github.com/Rust-for-Linux/linux/issues/1157.
> While rustc already lints missing documentation for public items, private items remain unchecked.
> This patch series aims to close that gap by ensuring proper documentation practices
> for private Rust items in the kernel.
> As underlined in this issue:
> https://github.com/Rust-for-Linux/linux/issues/1157, the purposes of
> this patch serie is to ensure the proper documentation of private rust
> items. Public items missing documentation are already linted by rustc.
>
> The actual solution comes in several parts
> ------------------------------------------
>
>  1) Patch 1 : Implements detection logic to emit warnings for improperly
>  documented private Rust items (e.g., // comments instead of ///).
>  2) Patch 2 : Adds an auto-fix mechanism via the --fix option to help
>  developers correct documentation issues.
>
> Results
> --------------------

Thanks for this helpful example, I'd recommend you to run your modified
version on real patches from the list and/or on already existing commits
in the kernel.

>
> The following implementation has been tested against this input file:
> // SPDX-License-Identifier: GPL-2.0

[...]

> pub struct Point2D {
>   pub x: f32,
>   pub y: f32
> }
>
> mod test_module {
>     // Module inner comment - should not trigger
> }
>
> // Comment before macro - should not trigger
> macro_rules! my_macro {

I think we should also trigger this for macros. All macros are private
by default and only made public with the `#[macro_export]` annotation.

>     // Comment inside macro - should not trigger
>     () => {};
> }
>
> // Comment before unsafe block - should not trigger
> unsafe {
>     // Comment inside unsafe block - should not trigger
>     let x = 5;
> }
>
> // Comment with unsafe word - should trigger
> fn with_unsafe_keyword() {
>     println!("test");
> }
>
> // Comment with code example: - should trigger
> // let x = 5; - should trigger

Code examples are usually wrapped in '```', so they are another
indicator that it probably should be a doc-comment.

> fn with_code_example() {
>     println!("test");
> }
>
> // NOTE: important consideration - should not trigger
> fn note_marker() -> bool {
>     true
> }
>
> // Comment with code example: - should trigger

I'm not 100% convinced that this should trigger. We have several cases
of the following (in some cases the function is public, I don't
remember if we have a private case):

    /// Normal function docs...
    // We probably should refactor XYZ.
    fn foo() {}

There it should not trigger.

---
Cheers,
Benno

> /// let x = 5; - should not trigger
> fn with_mixed_comments() {
>     println!("test");
> }


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 0/2] checkpatch.pl: add warning for // comments on private Rust items
@ 2025-04-19 19:20 Arnaud Lecomte
  0 siblings, 0 replies; 5+ messages in thread
From: Arnaud Lecomte @ 2025-04-19 19:20 UTC (permalink / raw)
  To: Andy Whitcroft, Joe Perches, Dwaipayan Ray, Lukas Bulwahn,
	Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Nathan Chancellor,
	Nick Desaulniers, Bill Wendling, Justin Stitt
  Cc: linux-kernel, rust-for-linux, llvm, contact, skhan

Hi,

Background
----------

The Rust-for-Linux project currently lacks enforcement of documentation for private Rust items, 
as highlighted in https://github.com/Rust-for-Linux/linux/issues/1157.
While rustc already lints missing documentation for public items, private items remain unchecked.
This patch series aims to close that gap by ensuring proper documentation practices
for private Rust items in the kernel.
As underlined in this issue:
https://github.com/Rust-for-Linux/linux/issues/1157, the purposes of
this patch serie is to ensure the proper documentation of private rust
items. Public items missing documentation are already linted by rustc. 

The actual solution comes in several parts
------------------------------------------

 1) Patch 1 : Implements detection logic to emit warnings for improperly
 documented private Rust items (e.g., // comments instead of ///).
 2) Patch 2 : Adds an auto-fix mechanism via the --fix option to help
 developers correct documentation issues.

Results
--------------------

The following implementation has been tested against this input file:
// SPDX-License-Identifier: GPL-2.0

// Simple comment - should not trigger

// Returns a reference to the underlying [`cpufreq::Table`]. - should trigger

fn table(&self) -> &cpufreq::Table {
    // SAFETY: The `ptr` is guaranteed by the C code to be valid. - should not trigger
    unsafe { cpufreq::Table::from_raw(self.ptr) }
}

// Improper doc comment for a private function. - should trigger
fn test() -> u32 {
    42
}

/// Proper doc comment for a private function. - should not trigger
fn proper_doc() -> u32 {
    42
}

// TODO: implement more logic - should not trigger
fn todo_marker() -> bool {
    true
}

// Just a regular comment not followed by code - should not trigger

pub fn public_function() {
    // Public function - should not trigger
}

// Test - should trigger

struct Point2D {
  pub x: f32,
  pub y: f32
}

// Test - should not trigger

pub struct Point2D {
  pub x: f32,
  pub y: f32
}

// Test - should not trigger

pub struct Point2D {
  pub x: f32,
  pub y: f32
}

mod test_module {
    // Module inner comment - should not trigger
}

// Comment before macro - should not trigger
macro_rules! my_macro {
    // Comment inside macro - should not trigger
    () => {};
}

// Comment before unsafe block - should not trigger
unsafe {
    // Comment inside unsafe block - should not trigger
    let x = 5;
}

// Comment with unsafe word - should trigger
fn with_unsafe_keyword() {
    println!("test");
}

// Comment with code example: - should trigger
// let x = 5; - should trigger
fn with_code_example() {
    println!("test");
}

// NOTE: important consideration - should not trigger
fn note_marker() -> bool {
    true
}

// Comment with code example: - should trigger
/// let x = 5; - should not trigger
fn with_mixed_comments() {
    println!("test");
}

which led to this output:
WARNING: Consider using `///` for private item documentation (line 5)

+// Returns a reference to the underlying [`cpufreq::Table`]. - should trigger
WARNING: Consider using `///` for private item documentation (line 12)

+// Improper doc comment for a private function. - should trigger
WARNING: Consider using `///` for private item documentation (line 33)

+// Test - should trigger
WARNING: Consider using `///` for private item documentation (line 74)

+// Comment with unsafe word - should trigger
WARNING: Consider using `///` for private item documentation (line 79)

+// Comment with code example: - should trigger
WARNING: Consider using `///` for private item documentation (line 80)

+// let x = 5; - should trigger
WARNING: Consider using `///` for private item documentation (line 91)

+// Comment with code example: - should trigger
total: 0 errors, 7 warnings, 95 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

test.rs has style problems, please review.

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.

Link: https://github.com/Rust-for-Linux/linux/issues/1157
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Arnaud Lecomte <contact@arnaud-lcm.com>
---
Arnaud Lecomte (2):
      checkpatch.pl: warn about // comments on private Rust items
      checkpatch.pl: add `--fix` support for `//` comments on private Rust items

 scripts/checkpatch.pl | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
---
base-commit: 250b341caf5e0e099dcf789be652178ca57a68cd
change-id: 20250419-checkpatch-rust-private-item-comment-bb3a5281e5da

Best regards,
-- 
Arnaud Lecomte <contact@arnaud-lcm.com>


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-04-20  8:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-19 20:24 [PATCH 0/2] checkpatch.pl: add warning for // comments on private Rust items Arnaud Lecomte
2025-04-19 21:24 ` [PATCH 1/2] checkpatch.pl: warn about " Arnaud Lecomte
2025-04-19 22:25   ` [PATCH 2/2] checkpatch.pl: add `--fix` support for `//` " Arnaud Lecomte
2025-04-20  8:15 ` [PATCH 0/2] checkpatch.pl: add warning for // " Benno Lossin
  -- strict thread matches above, loose matches on Subject: below --
2025-04-19 19:20 Arnaud Lecomte

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®