diff --git a/NEWS b/NEWS index a7a37f1c3..02bf6afc4 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,30 @@ GNU Bison NEWS * Noteworthy changes in release ?.? (????-??-??) [?] +*** Diagnostics for stylistic issues: -Wuseless-action + + A new warning category is introduced: 'style'. Currently it reports + useless explicit actions. For instance on the following grammar: + + %type "number" expr term + %% + expr: expr "+" term { $$ = $1 + $3; } + | term { $$ = $1; } + term: "(" expr ")" { $$ = $2; } + | "number" { $$ = $1; } + + bison diagnoses: + + $ bison -Wuseless-action foo.y + foo.y:4.21-32: warning: useless explicit action [-Wuseless-action] + | term { $$ = $1; } + ^~~~~~~~~~~~ + foo.y:6.21-32: warning: useless explicit action [-Wuseless-action] + | "number" { $$ = $1; } + ^~~~~~~~~~~~ + foo.y: warning: fix-its can be applied. Rerun with option '--update'. [-Wother] + + Running 'bison -Wuseless-action --update foo.y' would remove these actions. * Noteworthy changes in release 3.4.1 (2019-05-22) [stable] diff --git a/doc/bison.texi b/doc/bison.texi index 0301a96c2..f64c62997 100644 --- a/doc/bison.texi +++ b/doc/bison.texi @@ -10490,6 +10490,41 @@ One would get the exact same parser with the following directives instead: @end group @end example +@item useless-action +Useless explicit actions (@samp{@{ $$ = $1; @}}) are reported. For instance +on the following grammar: + +@example +@group +$ @kbd{cat foo.y} +%type "number" expr term +%% +expr: expr "+" term @{ $$ = $1 + $3; @} + | term @{ $$ = $1; @} +term: "(" expr ")" @{ $$ = $2; @} + | "number" @{ $$ = $1; @} +@end group +@end example + +@noindent +@command{bison} diagnoses: + +@example +$ @kbd{bison -Wuseless-action foo.y} +foo.y:4.21-32: warning: useless explicit action [-Wuseless-action] + | term @{ $$ = $1; @} + ^~~~~~~~~~~~ +foo.y:6.21-32: warning: useless explicit action [-Wuseless-action] + | "number" @{ $$ = $1; @} + ^~~~~~~~~~~~ +foo.y: warning: fix-its can be applied. Rerun with option '--update'. [-Wother] +@end example + +Running @kbd{bison -Wuseless-action --update foo.y} would remove these actions. + +Actions with named references (e.g., @samp{@{ $expr = $term; @}}) are not +reported. To disable the warning locally, write @samp{@{ $$ = ($1); @}}. + @item yacc Incompatibilities with POSIX Yacc. diff --git a/src/getargs.c b/src/getargs.c index 53ae161ab..3845bc07e 100644 --- a/src/getargs.c +++ b/src/getargs.c @@ -344,6 +344,7 @@ Warning categories include:\n\ 'empty-rule' empty rules without %empty\n\ 'midrule-values' unset or unused midrule values\n\ 'precedence' useless precedence and associativity\n\ + 'useless-action' useless explicit actions\n\ 'yacc' incompatibilities with POSIX Yacc\n\ 'other' all other warnings (enabled by default)\n\ 'all' all the warnings except 'yacc'\n\