Re: [PATCH v2] checkpatch: exclude forward declarations of const structs

From: Joe Perches

Date: Tue Mar 31 2026 - 14:45:15 EST


On Tue, 2026-03-31 at 14:15 -0400, Taylor Nelms wrote:
> Limit checkpatch warnings for normally-const structs by excluding
> patterns consistent with forward declarations.
>
> For example, the forward declaration `struct regmap_access_table;` in a
> header file currently generates a warning recommending that it is
> generally declared as const; however, this would apply a useless type
> qualifier in the empty declaration `const struct regmap_access_table;`,
> and subsequently generate compiler warnings.
>
> Signed-off-by: Taylor Nelms <[tknelms@xxxxxxxxxx](mailto:tknelms@xxxxxxxxxx)>

Acked-by: Joe Perches <joe@xxxxxxxxxxx>

> ---
>
> Changelog since v1:
> - Changed capturing group to character class in regex.
>
> scripts/checkpatch.pl | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index e56374662ff7..bed0bfedf5ba 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -7502,10 +7502,10 @@ sub process {
> }
>
> # check for various structs that are normally const (ops, kgdb, device_tree)
> -# and avoid what seem like struct definitions 'struct foo {'
> +# and avoid what seem like struct definitions 'struct foo {' or forward declarations 'struct foo;'
> if (defined($const_structs) &&
> $line !~ /\bconst\b/ &&
> - $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
> + $line =~ /\bstruct\s+($const_structs)\b(?!\s*[\{;])/) {
> WARN("CONST_STRUCT",
> "struct $1 should normally be const\n" . $herecurr);
> }
>
> ```