DotNetAnalyzers / NullParameterCheckRefactoring

Null Parameter Check Refactoring for Reference Type Parameters
MIT License
14 stars 4 forks source link

Preserve the parameter order when doing the null checks #7

Open tugberkugurlu opened 10 years ago

tugberkugurlu commented 10 years ago

Current implementation doesn't preserve the parameter order for the added if statements:

null-check-param-order-repro

AdamSpeight2008 commented 10 years ago

Potential Bug: What if the implement of the Interface is a structure?

tugberkugurlu commented 10 years ago

It is already covered with this, isn't it?

tugberkugurlu commented 10 years ago

@AdamSpeight2008 if you are talking about the below situation, it's safe AFAIK.

var bar = new FooBar(new Foo());

public class FooBar
{
    public FooBar(IFoo foo)
    {
        if (foo == null)
        {
            throw new ArgumentNullException(nameof(foo));
        }
    }
}

public interface IFoo
{
}

public struct Foo : IFoo
{
}
AdamSpeight2008 commented 10 years ago

I'm gonna have a look at this, as I think I have Idea how to implements. (I'll Branch the build)

tugberkugurlu commented 10 years ago

This is also applicable for VB implementation. @AdamSpeight2008 can you take a look at VB implementation for this, too?

AdamSpeight2008 commented 10 years ago

This is the basic idea was thinking of implementing.

Dim Guards As New List( )( Parameters.Count )

For Each ExistingGuard In ExistingGuards
  Dim Index = FindParameterIndexOfGuard( ExistingGuard ) 
  If Index.HasValue Then Guards( Index.Value ) = ExistingGuard
Next 
' Remove Existing Guards

' Add new Parameter Guard
  Dim NewGuard = CreateSingleLineIfGuard( parameter )
  Dim Index = FindParameterIndexOfGuard( NewGuard )
  If Index.HasValue Then Guards( Index.Value )

For Each Guard In Guards
  ' Insert Guard in method
Next

I'll try and get a working prototype later one.

sharwell commented 10 years ago

I lean towards this instead:

addNullCheck(x):
    insertionIndex = 0
    for i = 0...(x - 1)
        if isGuardForParameter(i, statements[insertionIndex])
            insertionIndex++
    statements.insert(insertionIndex, createGuard(x))
AdamSpeight2008 commented 10 years ago

Implemented for VB.net