-
Notifications
You must be signed in to change notification settings - Fork 30
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
Conversation
Place the comment on another line to avoid excessively long lines.
validate_links fails for unrelated reasons. |
There was a problem hiding this 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
There was a problem hiding this 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
store.emplace_back(shape); // Noncompliant, call to std::move is expected. | |
store.emplace_back(shape); // Noncompliant, calls Shape's copy constructor |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Co-authored-by: Loïc Joly <loic.joly@sonarsource.com>
There was a problem hiding this 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. |
There was a problem hiding this comment.
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.
Co-authored-by: Loïc Joly <loic.joly@sonarsource.com>
Quality Gate passed for 'rspec-frontend'Issues Measures |
Quality Gate passed for 'rspec-tools'Issues Measures |
Companion PR: SonarSource/sonar-cpp#3622