Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TooManyStatementsPerLine for lambdas #2865

Open
Nekto89 opened this issue Feb 12, 2025 · 3 comments
Open

TooManyStatementsPerLine for lambdas #2865

Nekto89 opened this issue Feb 12, 2025 · 3 comments
Labels

Comments

@Nekto89
Copy link
Contributor

Nekto89 commented Feb 12, 2025

Describe the bug
TooManyStatementsPerLine is shown for one-line short lambdas which I find strange.

To Reproduce
Analyze code that contains lambdas.
Examples:
const auto get_value = [](const auto& element) noexcept { return element.second; };
std::for_each(files.begin(), files.end(), [&](const auto& i_file) { new_files.AddFile(i_file); });

Expected behavior
In my opinion there should be no "cxx:TooManyStatementsPerLine" code smells in such cases.

Screenshots
Image

Desktop (please complete the following information):

  • OS: Windows
  • SonarQube version: 9.9.8
  • cxx plugin version: 2.1.3.851
  • sonar-scanner version: 5.0.1.3006
@guwirth
Copy link
Collaborator

guwirth commented Feb 12, 2025

Hi @Nekto89,

easy workaround would be (looks also much more readable):

const auto get_value = [](const auto& element) noexcept {
   return element.second;
};
std::for_each(files.begin(), files.end(), [&](const auto& i_file) {
   new_files.AddFile(i_file);
});

The check is implemented here, maybe you can have a look to it:

public class TooManyStatementsPerLineCheck extends AbstractOneStatementPerLineCheck<Grammar> {

public abstract class AbstractOneStatementPerLineCheck<G extends Grammar> extends SquidCheck<G> {

In the SSLR toolkit you can narrow it down. Maybe should only check inside statementSeq?

Image

Regards,

@guwirth
Copy link
Collaborator

guwirth commented Feb 15, 2025

How SonarSource is implementing this https://rules.sonarsource.com/cpp/RSPEC-122/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants