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 S2445: Add C# description and samples #1570

Merged
merged 15 commits into from Mar 1, 2023

Conversation

antonioaversa
Copy link
Contributor

@antonioaversa antonioaversa commented Feb 13, 2023

Implementation #6750

Kept as draft as it is to be merged after the implementation.

Narrowed scope for C#

The definition of the rule for C# has been narrowed during the review.
Outcome of the offline discussion:

  • it may make sense to have a lock on a field of a class different from the one where the lock is acquired, for example in a situation where locking objects are externalized and shared among multiple classes, for inter-class synchronization (e.g. a serial port lock);
  • so, requiring the readonly field to also be private appears in the end too constraining;
  • it also makes the rule too complex and non-atomic, and doesn't allow the user to opt in one behavior (lock on a readonly object), opting out the other (lock on a non-private object);
  • you may also want to have it protected, instead of private, to allow methods of a derived type to synchronize with methods of the base type, and you don't want to be forced to use properties to do that.

For these reasons, we are changing the rule and narrowing its scope for C#: when locking on a field, that field should be readonly. Whether it's private or not, it won't matter for the implementation of this rule.

@antonioaversa antonioaversa force-pushed the Antonio/S2445-blocks-synch-on-readonly-fields branch 2 times, most recently from ff5bc4b to e812315 Compare February 13, 2023 10:58
@antonioaversa antonioaversa force-pushed the Antonio/S2445-blocks-synch-on-readonly-fields branch from e812315 to 685319b Compare February 13, 2023 11:05
@sonarsource-next
Copy link

SonarQube Quality Gate for 'rspec-tools'

Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarsource-next
Copy link

SonarQube Quality Gate for 'rspec-frontend'

Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

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
One note: shouldn't this be applied to VB.NET as well? Locking works there exactly the same way as it does in C# (with the SyncLock keyword), and it seems like a simple rule.

@antonioaversa
Copy link
Contributor Author

antonioaversa commented Feb 13, 2023

@zsolt-kolbay-sonarsource

LGTM One note: shouldn't this be applied to VB.NET as well? Locking works there exactly the same way as it does in C# (with the SyncLock keyword), and it seems like a simple rule.

While only C# is mentioned in the task, it surely could. If you and @mary-georgiou-sonarsource both agree, I would invest some time extending the implementation to VB.NET as well.

@mary-georgiou-sonarsource
Copy link
Contributor

@zsolt-kolbay-sonarsource

LGTM One note: shouldn't this be applied to VB.NET as well? Locking works there exactly the same way as it does in C# (with the SyncLock keyword), and it seems like a simple rule.

While only C# is mentioned in the task, it surely could. If you and @mary-georgiou-sonarsource both agree, I would invest some time extending the implementation to VB.NET as well.

In case this rule is valid also for VB.NET (I haven't checked) - IMO, it's better to create another issue regarding the VB.NET version of the rule and implement the C# part in way that the implementation can be re-used for the VB.NET (by putting the common code in a base class).

@antonioaversa
Copy link
Contributor Author

@zsolt-kolbay-sonarsource

LGTM One note: shouldn't this be applied to VB.NET as well? Locking works there exactly the same way as it does in C# (with the SyncLock keyword), and it seems like a simple rule.

While only C# is mentioned in the task, it surely could. If you and @mary-georgiou-sonarsource both agree, I would invest some time extending the implementation to VB.NET as well.

In case this rule is valid also for VB.NET (I haven't checked) - IMO, it's better to create another issue regarding the VB.NET version of the rule and implement the C# part in way that the implementation can be re-used for the VB.NET (by putting the common code in a base class).

Reporting the outcome of the discussion offline on the possibility of extending the rule to VB.NET.

We are going to have a dedicated GitHub issue to implement this rule for VB.NET, rather than extending this GitHub issue.

We are going to stick with the C#-specific implementation for the time being, since the effort to refactor the code into a base class is possibly 90% or more, of the effort to implement the VB.NET rule.

Copy link
Contributor

@mary-georgiou-sonarsource mary-georgiou-sonarsource left a comment

Choose a reason for hiding this comment

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

Round 1:

You'll need to rebase on master for the pipeline to succeed.

rules/S2445/csharp/metadata.json Outdated Show resolved Hide resolved
rules/S2445/csharp/rule-except-see.adoc Outdated Show resolved Hide resolved
rules/S2445/csharp/rule-except-see.adoc Outdated Show resolved Hide resolved
rules/S2445/csharp/rule-except-see.adoc Outdated Show resolved Hide resolved
rules/S2445/csharp/rule-except-see.adoc Outdated Show resolved Hide resolved
Copy link
Contributor

@pavel-mikula-sonarsource pavel-mikula-sonarsource left a comment

Choose a reason for hiding this comment

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

Cleanup

rules/S2445/csharp/metadata.json Outdated Show resolved Hide resolved
rules/S2445/csharp/rule-except-see.adoc Outdated Show resolved Hide resolved
rules/S2445/csharp/rule-except-see.adoc Outdated Show resolved Hide resolved
rules/S2445/csharp/rule.adoc Outdated Show resolved Hide resolved
rules/S2445/csharp/rule-except-see.adoc Outdated Show resolved Hide resolved
rules/S2445/rule.adoc Show resolved Hide resolved
rules/S2445/csharp/rule.adoc Show resolved Hide resolved
rules/S2445/java/rule.adoc Outdated Show resolved Hide resolved
@antonioaversa
Copy link
Contributor Author

@pavel-mikula-sonarsource @mary-georgiou-sonarsource I addressed your comments and I am reassigning this PR back to you, although GitHub Checks are currently failing.

The issue with the GitHub Checks seems to come from https://next.sonarqube.com/sonarqube/batch/index returning 503, rather than by actual issues with the adoc content itself.

Copy link
Contributor

@mary-georgiou-sonarsource mary-georgiou-sonarsource left a comment

Choose a reason for hiding this comment

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

  • Refining a bit more the description
  • One file probably needs reverting
  • Please add the educational links

Copy link
Contributor

@mary-georgiou-sonarsource mary-georgiou-sonarsource left a comment

Choose a reason for hiding this comment

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

Just a bit more decription refinement - looks good otherwise.

@pavel-mikula-sonarsource
Copy link
Contributor

@pavel-mikula-sonarsource We have the already existing SonarSource/sonar-dotnet#6750.
Can't we use that one?

Yes, sure. I wasn't aware in what state the actual implementation PR is.

Copy link
Contributor

@pavel-mikula-sonarsource pavel-mikula-sonarsource left a comment

Choose a reason for hiding this comment

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

Final polishing

rules/S2445/csharp/message.adoc Outdated Show resolved Hide resolved
rules/S2445/csharp/message.adoc Outdated Show resolved Hide resolved
rules/S2445/csharp/message.adoc Outdated Show resolved Hide resolved
rules/S2445/csharp/metadata.json Outdated Show resolved Hide resolved
Copy link
Contributor

@pavel-mikula-sonarsource pavel-mikula-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

@pavel-mikula-sonarsource
Copy link
Contributor

Is there a ticket created for the part that was left out? It should be in sonar-dotnet,labeled with the new rule idea

Copy link
Contributor

@mary-georgiou-sonarsource mary-georgiou-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

@antonioaversa
Copy link
Contributor Author

antonioaversa commented Feb 27, 2023

Is there a ticket created for the part that was left out? It should be in sonar-dotnet,labeled with the new rule idea

Created SonarSource/sonar-dotnet#6804

@pavel-mikula-sonarsource

@sonarsource-next
Copy link

SonarQube Quality Gate for 'rspec-tools'

Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarsource-next
Copy link

SonarQube Quality Gate for 'rspec-frontend'

Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@antonioaversa antonioaversa marked this pull request as ready for review March 1, 2023 09:39
@antonioaversa antonioaversa merged commit 863b786 into master Mar 1, 2023
@antonioaversa antonioaversa deleted the Antonio/S2445-blocks-synch-on-readonly-fields branch March 1, 2023 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants