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 S3358 FP: Nested ternary operator is in a lambda #5774

Merged
merged 7 commits into from Jun 28, 2022

Conversation

martin-strecker-sonarsource
Copy link
Contributor

Fixes #5773

Todo

  • VB Support?

@martin-strecker-sonarsource martin-strecker-sonarsource added this to In progress in Best Kanban via automation Jun 24, 2022
@martin-strecker-sonarsource martin-strecker-sonarsource removed this from In progress in Best Kanban Jun 24, 2022
@Corniel
Copy link
Contributor

Corniel commented Jun 24, 2022

As mentioned at the Community Platform, I think this is not a false positive; it's about readability, and that does not change by having lamda's or not.

@martin-strecker-sonarsource martin-strecker-sonarsource changed the title S3358: False positive: Nested ternary operator is in a lambda Fix S3358 FP: Nested ternary operator is in a lambda Jun 27, 2022
@martin-strecker-sonarsource
Copy link
Contributor Author

The ITs found the following FPs

// 1.
                var configWithDefaultFallback = bootstrap.HasValue
                    ? bootstrap.Value.Config.Select(c => c == _defaultConfig ? c : c.WithFallback(_defaultConfig))
                    : _defaultConfig;
// 2.
            var messages = (SupportsAtomicPersistAllOfSeveralEvents
                ? Enumerable.Range(from, to - 1)
                    .Select(
                        i =>
                            i == to - 1
                                ? new AtomicWrite(
                                    new[] {persistent(i), persistent(i + 1)}.ToImmutableList<IPersistentRepresentation>())
                                : new AtomicWrite(persistent(i)))
                : Enumerable.Range(from, to).Select(i => new AtomicWrite(persistent(i))))
                .ToArray();

// 3.
            return empty != null
                ? new object[0]
                : ctors.First().GetParameters().Select(p => p.ParameterType.IsValueType ? Activator.CreateInstance(p.ParameterType) : null).ToArray();

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

"id": "S3358",
"message": "Extract this nested ternary operation into an independent statement.",
"location": {
"uri": "sources\akka.net\src\core\Akka.MultiNodeTestRunner\Discovery.cs",
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice

@@ -24,6 +24,14 @@ public bool FooImpl(bool isMale, bool isMarried)
? "Mrs. "
: "Miss ";

var lambda1 = true // Compliant. Nested in Lambda is valid
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add noncompliant

var lambda3 = new Func<string, string>(s => isMale ? "Mr. " : isMarried ? "Mrs. " : "Miss ")

@sonarcloud
Copy link

sonarcloud bot commented Jun 28, 2022

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 Jun 28, 2022

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

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@martin-strecker-sonarsource martin-strecker-sonarsource deleted the Martin/S3358_NestedTernary branch June 28, 2022 07:43
@martin-strecker-sonarsource
Copy link
Contributor Author

@martin-strecker-sonarsource
Copy link
Contributor Author

@andrei-epure-sonarsource I checked this on peach and found one nonreproducible issue (see above). Can we have a look together?

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.

Fix S3358 FP: Nested ternary operator is in a lambda
3 participants