Sonarlint false positive in Visual Studio 2017 RC4

661 views
Skip to first unread message

ke...@gosh.co.za

unread,
Mar 2, 2017, 12:10:25 PM3/2/17
to SonarLint
Hello,

I've recently been using SonarLint in VS2017 RC4 after using it in VS2015 for quite some time.

In the C# code example below, I am getting an alert for S1450 on line 3 as follows:
S1450 Remove the "_a" field and declare it as a local variable in the relevant methods.

public class PrivateFieldUsedAsLocalVariable
{
   
private readonly string _a;

   
public PrivateFieldUsedAsLocalVariable(string a)
   
{
        _a
= a;
   
}

   
public string GetValue()
   
{
       
return _a;
   
}
}

Using SonarLint v2.11.0.1102 this only happens in VS2017 and not in VS2015. It also happened in v2.10, also only on VS2017.

Note that running the sonaranalyzer-dotnet tests in the same installation of VS2017 passes correctly - the problem is only evident when actually analysing code in VS2017.

Regards,
Kevin

SJC

unread,
Mar 3, 2017, 4:46:32 AM3/3/17
to SonarLint, ke...@gosh.co.za
We are having a few examples of the same issue too.

public class ClassName : BaseClassName
{
        private readonly Regex escapePattern;
 
        public ClassName(string location) : base(location)
        {
            // Regex may change depending on location
             escapePattern = new Regex("Blah");
        }

        private string Escape(string value)
        {
            if (Check)
            {
                value = escapePattern.Replace(value, m => escapes[m.Value]);
            }
 
            return value;
        }
}

In my mind if the variable is defined as a Readonly and is used in multiple places within the file S1450 should not be thrown.
I notice that this rule has been available since 16th December which is why we are noticing it now.

If its any use I too am using VS2017 RC4 and SonarQube 6.2

Amaury Leve

unread,
Mar 7, 2017, 9:06:08 AM3/7/17
to SJC, SonarLint, ke...@gosh.co.za
Hi guys,

Thanks for spotting this.
We have created a ticket (https://jira.sonarsource.com/browse/SVS-141) and we will try to work on it as soon as possible.

Cheers,
DotNet Team

--
You received this message because you are subscribed to the Google Groups "SonarLint" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarlint+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarlint/6289e277-fe3b-48fe-b080-a2800ffb52bf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Amaury LEVE | SonarSource
Software Developer
Reply all
Reply to author
Forward
0 new messages