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

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

There is a newer version: 10.2.0.105762
Show newest version

Why is this an issue?

When the syntax new Guid() (i.e. parameterless instantiation) is used, it must be that one of three things is wanted:

  1. An empty GUID, in which case Guid.Empty is clearer.
  2. A randomly-generated GUID, in which case Guid.NewGuid() should be used.
  3. A new GUID with a specific initialization, in which case the initialization parameter is missing.

This rule raises an issue when a parameterless instantiation of the Guid struct is found.

Noncompliant code example

public void Foo()
{
    var g1 = new Guid();    // Noncompliant - what's the intent?
    Guid g2 = new();        // Noncompliant
    var g3 = default(Guid); // Noncompliant
    Guid g4 = default;      // Noncompliant
}

Compliant solution

public void Foo(byte[] bytes)
{
    var g1 = Guid.Empty;
    var g2 = Guid.NewGuid();
    var g3 = new Guid(bytes);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy