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

Fix S2094 FP: Primary constructors for classes should be handled equal to records #9019

Merged

Conversation

Corniel
Copy link
Contributor

@Corniel Corniel commented Apr 3, 2024

Consider the following piece of code:

class ChildClass() : BaseClass(42){ }

class BaseClass(int value) { }

Than ChildClass should not be considered an empty class, and hence should not raise S2094,

I'm in doubt if S2094 should raise on BaseClass, but you could argue that there should be a different rule, that checks if the parameters of a primary constructor (on classes only) is actually used.

@sebastien-marichal
Copy link
Contributor

@Corniel can you rebase your PR, the test case has been added with #9024.

@sebastien-marichal sebastien-marichal self-assigned this Apr 8, 2024
@sebastien-marichal sebastien-marichal added Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. labels Apr 8, 2024
@zsolt-kolbay-sonarsource
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@zsolt-kolbay-sonarsource
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource left a comment

Choose a reason for hiding this comment

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

Nice fix! Left some polishing comments.

|| IsParameterlessRecord(node);

private static bool IsParameterlessClass(SyntaxNode node) =>
ClassDeclarationSyntax(node) is { } declaration
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just use the ClassDeclarationSyntax type directly? It's available without a wrapper.
The only change that it will need is to replace the declaration.ParameterList property with the declaration.ParameterList() method call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zsolt-kolbay-sonarsource I thought I tried that, and that it did not work. Anyhow, now it does.

&& LacksParameters(declaration.ParameterList, declaration.BaseList);

private static bool LacksParameters(ParameterListSyntax parameterList, BaseListSyntax baseList) =>
parameterList?.Parameters is not { Count: >= 1 }
Copy link
Contributor

Choose a reason for hiding this comment

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

[Optional] Count: > 0 is slightly less of a cognitive load for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was { Count : >= 1 } at other places in this class, so I kept it the same. Now I updated all here to your suggestion.

@@ -18,6 +18,8 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Xml.Linq;
Copy link
Contributor

Choose a reason for hiding this comment

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

Not needed.

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 hate it when Visual Studio adds usings without telling you. It has been removed now.

@zsolt-kolbay-sonarsource
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link

sonarcloud bot commented Apr 9, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

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

See analysis details on SonarCloud

Copy link

sonarcloud bot commented Apr 9, 2024

Copy link
Contributor

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for the fix, and for the additional cleanup.

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource merged commit e8fb8ab into SonarSource:master Apr 9, 2024
34 checks passed
@Corniel Corniel deleted the corniel/fp-s2094 branch April 9, 2024 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix S2094 FP: Primary constructor calling base class constructor
3 participants