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

Modify rule S5500: mention std::ranges::move and rewrite RSPEC (CPP-5219) #3933

Merged
merged 15 commits into from
May 23, 2024

Conversation

marco-antognini-sonarsource
Copy link
Contributor

Companion PR: SonarSource/sonar-cpp#3622

@marco-antognini-sonarsource marco-antognini-sonarsource added the cfamily C / C++ / Objective-C label May 15, 2024
@marco-antognini-sonarsource
Copy link
Contributor Author

validate_links fails for unrelated reasons.

@marco-antognini-sonarsource marco-antognini-sonarsource marked this pull request as ready for review May 15, 2024 16:41
Copy link
Contributor

@loic-joly-sonarsource loic-joly-sonarsource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll let you decide if you want to apply the suggestion

rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
Copy link
Contributor

@loic-joly-sonarsource loic-joly-sonarsource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the rewrite a lot.

----
void DrawingStore::insertVisibleShape(Shape&& shape) {
if (shape.isVisible()) {
store.emplace_back(shape); // Noncompliant, call to std::move is expected.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
store.emplace_back(shape); // Noncompliant, call to std::move is expected.
store.emplace_back(shape); // Noncompliant, calls Shape's copy constructor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to highlight in the code snippet the root cause of the issue as generically as possible.

For example, this would be compliant:

if (shape.isVisible()) { store.emplace_back(shape); }
foo(std::move(shape));

because std::move is called.

In a way, we don't care about the copy/move constructors.

The sentence below is meant to explain what is actually happening and justify why std::move is required. It also serves to connect the two examples.

Does that make sense?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, what would you think of:

void DrawingStore::insertVisibleShape(Shape&& shape) { // Noncompliant, Shape is never moved
  if (shape.isVisible()) {
    store.emplace_back(shape); // Calls Shape's copy constructor.
  }
}

Which would be corrected as:

void DrawingStore::insertVisibleShape(Shape&& shape) { // Compliant
  if (shape.isVisible()) {
    store.emplace_back(std::move(shape)); // Calls Shape's move constructor.
  }
}

To me, the benefits are:

  • As you mention, even if a call to std::move is expected, it could be called in other places
  • I think it more clearly describes what actually happens in the code because:
    • Inline comments avoid having to refer to a place in the code in the next sentence
    • There is a symmetry in the inline comments that highlight the difference

If you still disagree, I'm not going to fight. The RSPEC is correct as it is. I just feel it could be improved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your idea is not bad, but it would take me a bit of time to adapt and make other sections consistent.

It also has a downside: the diff highlighting will include the first line of the (non)compliant code because of the comment, but only the comment changes. Since the diff is on the whole line (and not on words), this will distract the readers a bit.

So I'll keep the current wording for now. We can always revisit this in the future.

rules/S5500/cfamily/rule.adoc Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
Co-authored-by: Loïc Joly <loic.joly@sonarsource.com>
rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
Copy link
Contributor

@loic-joly-sonarsource loic-joly-sonarsource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still suggesting changes, but feel free to discard them if you disagree with them.

----
void DrawingStore::insertVisibleShape(Shape&& shape) {
if (shape.isVisible()) {
store.emplace_back(shape); // Noncompliant, call to std::move is expected.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, what would you think of:

void DrawingStore::insertVisibleShape(Shape&& shape) { // Noncompliant, Shape is never moved
  if (shape.isVisible()) {
    store.emplace_back(shape); // Calls Shape's copy constructor.
  }
}

Which would be corrected as:

void DrawingStore::insertVisibleShape(Shape&& shape) { // Compliant
  if (shape.isVisible()) {
    store.emplace_back(std::move(shape)); // Calls Shape's move constructor.
  }
}

To me, the benefits are:

  • As you mention, even if a call to std::move is expected, it could be called in other places
  • I think it more clearly describes what actually happens in the code because:
    • Inline comments avoid having to refer to a place in the code in the next sentence
    • There is a symmetry in the inline comments that highlight the difference

If you still disagree, I'm not going to fight. The RSPEC is correct as it is. I just feel it could be improved.

rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Show resolved Hide resolved
rules/S5500/cfamily/rule.adoc Outdated Show resolved Hide resolved
Co-authored-by: Loïc Joly <loic.joly@sonarsource.com>
Copy link

Quality Gate passed Quality Gate passed for 'rspec-frontend'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

Copy link

Quality Gate passed Quality Gate passed for 'rspec-tools'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarQube

@marco-antognini-sonarsource marco-antognini-sonarsource changed the title Modify rule S5500: mention std::ranges::move (CPP-5219) Modify rule S5500: mention std::ranges::move and rewrite RSPEC (CPP-5219) May 23, 2024
@marco-antognini-sonarsource marco-antognini-sonarsource merged commit fbcc8c7 into master May 23, 2024
10 of 11 checks passed
@marco-antognini-sonarsource marco-antognini-sonarsource deleted the mb/CPP-5219-S5500 branch May 23, 2024 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cfamily C / C++ / Objective-C
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants