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
New rule S4663: Comments should not be empty #6672
Conversation
23bfc2a
to
abb445a
Compare
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.
Good approach. I think Partition can be made a bit more readable and we need to be careful to allocate not too much. Some pieces need to be shifted around between the base class and the derived classes.
analyzers/src/SonarAnalyzer.CSharp/Rules/CommentsShouldNotBeEmpty.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/CommentsShouldNotBeEmpty.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/CommentsShouldNotBeEmpty.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/CommentsShouldNotBeEmpty.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/CommentsShouldNotBeEmpty.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/CommentsShouldNotBeEmptyBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/CommentsShouldNotBeEmptyBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.VisualBasic/Rules/CommentsShouldNotBeEmpty.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/CommentsShouldNotBeEmptyBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/CommentsShouldNotBeEmptyBase.cs
Outdated
Show resolved
Hide resolved
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.
Some annotations.
analyzers/src/SonarAnalyzer.Common/Rules/CommentsShouldNotBeEmptyBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/CommentsShouldNotBeEmptyBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/CommentsShouldNotBeEmptyBase.cs
Outdated
Show resolved
Hide resolved
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.
Nice. The IsKind/IsAnyKind approach looks good.
analyzers/src/SonarAnalyzer.Common/Rules/CommentsShouldNotBeEmptyBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/CommentsShouldNotBeEmptyBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/CommentsShouldNotBeEmptyBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/CommentsShouldNotBeEmptyBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/CommentsShouldNotBeEmptyBase.cs
Outdated
Show resolved
Hide resolved
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.
Some cosmetic changes and we should be fine.
analyzers/its/expected/Ember-MM/EmberAPI-{208AA35E-C6AE-4D2D-A9DD-B6EFD19A4279}-S4663.json
Show resolved
Hide resolved
...rs/its/expected/Ember-MM/scraper.EmberCore-{EF6A550E-DD76-4F4D-8250-8598140F828B}-S4663.json
Show resolved
Hide resolved
CheckTrivia(c, token.LeadingTrivia); | ||
CheckTrivia(c, token.TrailingTrivia); |
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.
CheckTrivia(c, token.LeadingTrivia); | |
CheckTrivia(c, token.TrailingTrivia); | |
// Hotpath: Don't allocate the trivia enumerable if not needed | |
if (token.HasLeadingTrivia) | |
{ | |
CheckTrivia(c, token.LeadingTrivia); | |
} | |
if (token.HasTrailingTrivia) | |
{ | |
CheckTrivia(c, token.TrailingTrivia); | |
} |
|
||
foreach (var trivium in trivia) | ||
{ | ||
if (IsSimpleComment(trivium)) // put it on the current block of "//" |
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.
The method name AddTriviaToPartition
says it already.
if (IsSimpleComment(trivium)) // put it on the current block of "//" | |
if (IsSimpleComment(trivium)) |
if (IsSimpleComment(trivium)) // put it on the current block of "//" | ||
{ | ||
AddTriviaToPartition(ref current, trivium); | ||
firstEndOfLineFound = false; |
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.
firstEndOfLineFound = false;
can be moved to AddTriviaToPartition.
firstEndOfLineFound = false; |
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/CommentsShouldNotBeEmpty.cs
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/CommentsShouldNotBeEmpty.vb
Show resolved
Hide resolved
CloseCurrentPartition(ref current, ref partitions, ref firstEndOfLineFound); | ||
} | ||
// This handles an empty line, for example: | ||
// // some comment \n <- EOL found, set to true |
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.
firstEndOfLineFound should be mentioned once to make clear what the "true/false" refers to.
// // some comment \n <- EOL found, set to true | |
// // some comment \n <- EOL found, firstEndOfLineFound set to true |
current.Add(trivia); | ||
} | ||
|
||
// Hotpath: Don't capture variables |
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.
Should be on the first local function.
// Hotpath: Don't capture variables |
CloseCurrentPartition(ref current, ref partitions, ref firstEndOfLineFound); | ||
return partitions; | ||
|
||
static void AddTriviaToPartition(ref List<SyntaxTrivia> current, SyntaxTrivia trivia) |
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.
static void AddTriviaToPartition(ref List<SyntaxTrivia> current, SyntaxTrivia trivia) | |
// Hotpath: Don't capture variables. | |
static void AddTriviaToPartition(ref List<SyntaxTrivia> current, SyntaxTrivia trivia) |
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.
LGTM. But one small issue still to be resoved.
public SyntaxKind RightShiftAssignmentStatement => SyntaxKind.RightShiftAssignmentExpression; | ||
public SyntaxKind RightShiftExpression => SyntaxKind.RightShiftExpression; | ||
public SyntaxKind RefKeyword => SyntaxKind.RefKeyword; | ||
public SyntaxKind ReturnStatement => SyntaxKind.ReturnStatement; |
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.
The order is mixed up.
4cfc1a7
to
99f9c80
Compare
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
Fixes #6663