Visual Studio 2010 Code Analysis CA1305 (Specify IFormatProvider) rule doesn't work for TryParse methods
Asked Answered
I

1

7

I am wondering why Code Analysis rule CA1305 - Specify IFormatProvider doesn't work for TryParse methods?

For example with 'Microsoft All Rules' rule set, Code Analysis gives CA1305 warning for following code:

static void Main(string[] args)
{
    string text = args[0];

    double value = double.Parse(text);
}

but doesn't give CA1305 warning for following code:

static void Main(string[] args)
{
    string text = args[0];

    double value;
    if (!double.TryParse(text, out value))
        value = 0;
}

I found this to be very unfortunate because TryParse methods are the correct way to do parsing if input string is not reliable.

Does anyone know if CA1305 rule for TryParse methods is implemented in some newer version of Code Analysis tool or by some 3rd party?

Incomprehensible answered 10/10, 2012 at 12:49 Comment(1)
There's a Connect Bug for this.Bedspread
U
0

This has since been fixed in FxCop as well as in more recent tools like SonarLint.

Unripe answered 17/11, 2016 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.