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

Create rule S3063: Add vbnet language #1524

Merged
merged 10 commits into from
Feb 15, 2023

Conversation

github-actions[bot]
Copy link
Contributor

You can preview this rule here (updated a few minutes after each push).

@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

@@ -1,16 +1,77 @@
include::../rule.adoc[]
``++StringBuilder++`` instances that are never ``++ToString++``ed needlessly clutter the code, and worse are a drag on performance. Either they should be removed, or the missing ``++ToString++`` call added.

Choose a reason for hiding this comment

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

Suggested change
``++StringBuilder++`` instances that are never ``++ToString++``ed needlessly clutter the code, and worse are a drag on performance. Either they should be removed, or the missing ``++ToString++`` call added.
`StringBuilder` instances that are never `ToString`ed needlessly clutter the code, and worse are a drag on performance. Either they should be removed, or the missing `ToString` call added.

(visible only on this page)
[source,csharp]
----
public void doSomething(List<string> strings) {

Choose a reason for hiding this comment

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

Suggested change
public void doSomething(List<string> strings) {
public void DoSomething(List<string> strings) {

(visible only on this page)
[source,csharp]
----
public void doSomething(List<string> strings) {

Choose a reason for hiding this comment

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

Suggested change
public void doSomething(List<string> strings) {
public void DoSomething(List<string> strings) {

or
[source,csharp]
----
public void doSomething(List<string> strings) {

Choose a reason for hiding this comment

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

Suggested change
public void doSomething(List<string> strings) {
public void DoSomething(List<string> strings) {

[source,csharp]
----
public void doSomething(List<string> strings) {
StringBuilder sb = new StringBuilder(); // Noncompliant

Choose a reason for hiding this comment

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

Suggested change
StringBuilder sb = new StringBuilder(); // Noncompliant
StringBuilder sb = new StringBuilder();

@@ -0,0 +1,75 @@
``++StringBuilder++`` instances that are never ``++ToString++``ed needlessly clutter the code, and worse are a drag on performance. Either they should be removed, or the missing ``++ToString++`` call added.

Choose a reason for hiding this comment

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

Suggested change
``++StringBuilder++`` instances that are never ``++ToString++``ed needlessly clutter the code, and worse are a drag on performance. Either they should be removed, or the missing ``++ToString++`` call added.
`StringBuilder` instances that are never `ToString`ed needlessly clutter the code, and worse are a drag on performance. Either they should be removed, or the missing `ToString` call added.


[source,vbnet]
----
Public Sub doSomething(ByVal strings As List(Of String))

Choose a reason for hiding this comment

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

Suggested change
Public Sub doSomething(ByVal strings As List(Of String))
Public Sub DoSomething(ByVal strings As List(Of String))


[source,vbnet]
----
Public Sub doSomething(ByVal strings As List(Of String))

Choose a reason for hiding this comment

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

Suggested change
Public Sub doSomething(ByVal strings As List(Of String))
Public Sub DoSomething(ByVal strings As List(Of String))

or
[source,vbnet]
----
Public Sub doSomething(ByVal strings As List(Of String))

Choose a reason for hiding this comment

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

Suggested change
Public Sub doSomething(ByVal strings As List(Of String))
Public Sub DoSomething(ByVal strings As List(Of String))

Comment on lines 43 to 45
No issue is reported when ``++StringBuilder++`` is:

* passed as method arguments, on the grounds that it will likely ``++ToString++``ed there.

Choose a reason for hiding this comment

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

Suggested change
No issue is reported when ``++StringBuilder++`` is:
* passed as method arguments, on the grounds that it will likely ``++ToString++``ed there.
No issue is reported when `StringBuilder` is:
* passed as method arguments, on the grounds that it will likely `ToString`ed there.

@@ -1,16 +1,58 @@
include::../rule.adoc[]
`StringBuilder` instances that are never `ToString`ed needlessly clutter the code, and worse are a drag on performance. Either they should be removed, or the missing `ToString` call added.

Choose a reason for hiding this comment

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

https://discuss.sonarsource.com/t/csharpsquid-s3063-description-seems-weird/13189 is complaining about

ToStringed

being odd and I agree. Can you find a better wording?

var sb = new StringBuilder(); // Compliant
sb.GetChunks();
----
* passed as method argument, on the grounds that it will likely `ToString`ed there.

Choose a reason for hiding this comment

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

ToStringed


No issue is reported when `StringBuilder` is:

* accessed through a ``CopyTo()`` or ``GetChunks()`` invocation.

Choose a reason for hiding this comment

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

Suggested change
* accessed through a ``CopyTo()`` or ``GetChunks()`` invocation.
* accessed through a `CopyTo()` or `GetChunks()` invocation.

[source,csharp]
----
var sb = new StringBuilder(); // Compliant
sb.GetChunks();

Choose a reason for hiding this comment

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

I don't think we need this example. The description is self-explanatory.

sb.Append(str).Append(", ");
// ...
}
_logger.LogInformation(sb.toString, DateTimeOffset.UtcNow);

Choose a reason for hiding this comment

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

The ILogger.LogInformation doesn't require a DateTimeOffset.UtcNow to be passed.

Suggested change
_logger.LogInformation(sb.toString, DateTimeOffset.UtcNow);
_logger.LogInformation(sb.toString);

Comment on lines 53 to 57
* retrieved by a custom function
[source,csharp]
----
var sb = GetStringBuilder(); // Compliant
----

Choose a reason for hiding this comment

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

Suggested change
* retrieved by a custom function
[source,csharp]
----
var sb = GetStringBuilder(); // Compliant
----
* retrieved by a custom function (`var sb = GetStringBuilder();`)


No issue is reported when `StringBuilder` is:

* accessed through a ``CopyTo()`` or ``GetChunks()`` invocation.

Choose a reason for hiding this comment

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

All items in a list should start with an uppercase and end with a dot (see e.g. https://github.com/SonarSource/rspec/blob/1b5fe36f3f98b48a9db7966ff6b6402d3db02242/rules/S6472/implementation.adoc)

Suggested change
* accessed through a ``CopyTo()`` or ``GetChunks()`` invocation.
* Accessed through a ``CopyTo()`` or ``GetChunks()`` invocation.


No issue is reported when `StringBuilder` is:

* accessed through a ``CopyTo()`` or ``GetChunks()`` invocation.

Choose a reason for hiding this comment

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

Same as for C#

@@ -1,16 +1,49 @@
include::../rule.adoc[]
`StringBuilder` instances that are not accessed through a `ToString()` invocation needlessly clutter the code, and worse are a drag on performance. Either they should be removed, or the missing `ToString()` call should be added.

Choose a reason for hiding this comment

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

Maybe like this?

Suggested change
`StringBuilder` instances that are not accessed through a `ToString()` invocation needlessly clutter the code, and worse are a drag on performance. Either they should be removed, or the missing `ToString()` call should be added.
`StringBuilder` instances that never build a `string` clutter the code and worse are a drag on performance. Either they should be removed, or the missing `ToString()` call should be added.

Choose a reason for hiding this comment

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

Yes that's better, thanks

sb.Append(str).Append(", ");
// ...
}
_logger.LogInformation(sb.toString);

Choose a reason for hiding this comment

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

I think the _ is a violation of one of our rules.

Suggested change
_logger.LogInformation(sb.toString);
logger.LogInformation(sb.ToString());


* Accessed through a `CopyTo()` or `GetChunks()` invocation.
* Passed as method argument, on the grounds that it will likely accessed through a `ToString()` invocation there.
* A parameter of the current method.

Choose a reason for hiding this comment

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

Suggested change
* A parameter of the current method.
* Passed in as a parameter to the current method, on the grounds that the callee will materialize the string.

No issue is reported when `StringBuilder` is:

* Accessed through a `CopyTo()` or `GetChunks()` invocation.
* Passed as method argument, on the grounds that it will likely accessed through a `ToString()` invocation there.

Choose a reason for hiding this comment

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

Suggested change
* Passed as method argument, on the grounds that it will likely accessed through a `ToString()` invocation there.
* Passed as a method argument, on the grounds that it will likely be accessed through a `ToString()` invocation there.


No issue is reported when `StringBuilder` is:

* Accessed through a `CopyTo()` or `GetChunks()` invocation.

Choose a reason for hiding this comment

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

Should we add the Length access and the indexer access?

Choose a reason for hiding this comment

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

I don't know; I was wondering the same. I decided to skip them to simplify the exception paragraph since both are not that common, but I might be wrong.

Choose a reason for hiding this comment

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

Maybe like this?

Suggested change
* Accessed through a `CopyTo()` or `GetChunks()` invocation.
* Accessed through `sb.CopyTo()`, `sb.GetChunks()`, `sb.Length`, or `sb[index]`.

Choose a reason for hiding this comment

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

Since we get rid of the exception examples it's not that complicated anymore so maybe we can add them for clarity.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I only just saw your comment. I created a sub-list for them, but your suggestion may be better indeed, let me change that real quick.

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM. Some typos to fix left.

sb.Append(str).Append(", ")
Next

My.Application.Log.WriteEntry(sb.toString())

Choose a reason for hiding this comment

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

Suggested change
My.Application.Log.WriteEntry(sb.toString())
My.Application.Log.WriteEntry(sb.ToString())

No issue is reported when `StringBuilder` is:

* Accessed through `sb.CopyTo()`, `sb.GetChunks()`, `sb.Length`, or `sb(index)`.
* Passed as a method arguments, on the grounds that it will likely be accessed through a `ToString()` invocation there.

Choose a reason for hiding this comment

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

Suggested change
* Passed as a method arguments, on the grounds that it will likely be accessed through a `ToString()` invocation there.
* Passed as a method argument, on the grounds that it will likely be accessed through a `ToString()` invocation there.

@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

@cristian-ambrosini-sonarsource cristian-ambrosini-sonarsource deleted the rule/S3063-add-vbnet branch February 15, 2023 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants