All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.csharp.S1848.html Maven / Gradle / Ivy

There is a newer version: 10.2.0.105762
Show newest version

Why is this an issue?

Creating objects that are not used is a vulnerability that can lead to unexpected behavior.

If this was done intentionally due to side effects in the object’s constructor, the code should be moved to a dedicated method.

How to fix it

Code examples

Noncompliant code example

public void Method(MyObject myObject)
{
    if (myObject is null)
    {
        new MyObject(); // Noncompliant
    }

    if (myObject.IsCorrupted)
    {
        new ArgumentException($"{nameof(myObject)} is corrupted"); // Noncompliant
    }

    // ...
}

Compliant solution

public void Method(MyObject myObject)
{
    if (myObject is null)
    {
        myObject = new MyObject(); // Compliant
    }

    if (myObject.IsCorrupted)
    {
        throw new ArgumentException($"{nameof(myObject)} is corrupted"); // Compliant
    }

    // ...
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy