LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 44326 - readability-const-return-type identifies the wrong `const` token that qualifies the return type
Summary: readability-const-return-type identifies the wrong `const` token that qualifi...
Status: RESOLVED FIXED
Alias: None
Product: clang-tools-extra
Classification: Unclassified
Component: clang-tidy (show other bugs)
Version: unspecified
Hardware: PC All
: P normal
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-12-17 13:43 PST by Ilya Mirsky
Modified: 2020-01-04 14:45 PST (History)
4 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ilya Mirsky 2019-12-17 13:43:10 PST
For the following code located in const-ret-type.cpp:

  const int* const volatile foo() {}

The following invocation of clang-tidy produces the following output:

  $ clang-tidy -checks=-*,readability-const-return-type const-ret-type.cpp
  2 warnings generated.
  const-ret-type.cpp:1:1: warning: return type 'const int *const volatile' is 'const'-qualified at the top level, ...
  const int* const volatile foo() {}
  ^~~~~~
  Suppressed 1 warnings (1 with check filters).

While the expected diagnostic should be:

  const int* const volatile foo() {}
               ^~~~~~

The readability-const-return-type checker incorrectly flags the first `const` token, but it's actually the other `const` token that precedes `volatile` that qualifies the return type. Applying the suggested fixit can result in a miscompilation. The culprit is in the implementation of the utils::lexer::getConstQualifyingToken function. I have a patch ready and I plan to submit it for review shortly.
Comment 1 Ilya Mirsky 2019-12-19 08:27:43 PST
A fix is up for a review: https://reviews.llvm.org/D71666
Comment 2 Jonas Toth 2020-01-04 14:45:04 PST
fixede with https://reviews.llvm.org/rGf58f39137c6e5a324ef684b1d72bddae244aa94d

Thank you for the patch!