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

New rule S6603: The collection-specific "TrueForAll" method should be used instead of the "All" extension #7163

Merged
merged 20 commits into from
May 9, 2023

Conversation

gregory-paidis-sonarsource
Copy link
Contributor

Fixes #7122

"location": {
"uri": "sources\Automapper\src\AutoMapper\Configuration\MapperConfiguration.cs",
"region": {
"startLine": 473,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

"id": "S6603",
"message": "The collection-specific "TrueForAll" method should be used instead of the "All" extension",
"location": {
"uri": "sources\Automapper\src\AutoMapper\Execution\ObjectFactory.cs",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

var ctorWithOptionalArgs = type.GetDeclaredConstructors().FirstOrDefault(c => c.GetParameters().All(p => p.IsOptional));

"id": "S6603",
"message": "The collection-specific "TrueForAll" method should be used instead of the "All" extension",
"location": {
"uri": "sources\Automapper\src\AutoMapper\Internal\TypeDetails.cs",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

public bool AllParametersOptional() => Parameters.All(p => p.IsOptional);

"location": {
"uri": "sources\Nancy\src\Nancy\Security\ClaimsPrincipalExtensions.cs",
"region": {
"startLine": 31,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

return user != null && requiredClaims.All(user.HasClaim);

"id": "S6603",
"message": "The collection-specific "TrueForAll" method should be used instead of the "All" extension",
"location": {
"uri": "sources\Nancy\src\Nancy\Security\ClaimsPrincipalExtensions.cs",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

return user != null && requiredRoles != null && requiredRoles.All(user.IsInRole);

Copy link
Contributor

@sebastien-marichal sebastien-marichal left a comment

Choose a reason for hiding this comment

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

Left some very small comments

Copy link
Contributor

@sebastien-marichal sebastien-marichal left a comment

Choose a reason for hiding this comment

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

LGTM! Only waiting for the TryGetOperands now 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a pretty good test coverage 👍 only 2 more things

public void UseTrueForAll_CS_Immutable() =>
builderCS.AddPaths("UseTrueForAll.Immutable.cs")
.AddReferences(MetadataReferenceFacade.SystemCollections)
.WithOutputKind(OutputKind.DynamicallyLinkedLibrary)

Choose a reason for hiding this comment

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

Educational: what is the purpose of this?

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 do not have a main or top level statements, I split my logic in classes. So the compiler needs to know that there is no entrypoint for this compilation (aka it's a library).

If I added a single line of code, like: var x = 42; above my code it would work as an executable, but it would not add much to the logic of the testcase.

Choose a reason for hiding this comment

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

Actually, I'm not sure, all of those UTs are running fine without this line. I think you need the "single line of code" only using WithTopLevelStatements where the compiler actually needs an entry point, but I think the functionality you mentioned is the default one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are absolutely right, the default behavior of the builder is DynamicLinkedLibrary. I did not see it because before I tried with top level statements, which as you said need at least a line of code. Thanks!

var nullableMethodBad = DoWork()?.All(x => true); // Noncompliant
var inlineInitialization = new int[] { 42 }.All(x => true); // Noncompliant

_ = immutable.Fluent().Fluent().Fluent().Fluent().All(x => true); //Noncompliant

Choose a reason for hiding this comment

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

Assignments are not needed and IMO they can be removed to visually simplify the tests

@sonarcloud
Copy link

sonarcloud bot commented May 8, 2023

Kudos, SonarCloud Quality Gate passed!    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

@sonarcloud
Copy link

sonarcloud bot commented May 8, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

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

95.5% 95.5% Coverage
0.0% 0.0% Duplication

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM! I'll leave you merge it when IsCorrectCall coverage is improved/ the method is modified removing the null-check on Symbol

@gregory-paidis-sonarsource gregory-paidis-sonarsource marked this pull request as ready for review May 9, 2023 10:29
@gregory-paidis-sonarsource gregory-paidis-sonarsource merged commit c325920 into master May 9, 2023
@gregory-paidis-sonarsource gregory-paidis-sonarsource deleted the greg/implement-S6603 branch May 9, 2023 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New rule S6603: The collection-specific "TrueForAll" method should be used instead of the "All" extension
3 participants