none
Code Analyzer and TryParse

    Question

  • Code Analyzer reports a potential bug, when e.g. Double.Parse is used without providing IFormatProvider, but doesn't issue the same warning for Double.TryParse function.

    Mikhail

    Friday, May 17, 2013 7:13 AM

Answers

All replies

  • Hi Mikhail,

    Glad to receive your reply.

    Double.Parse is used without providing IFormatProvider, but doesn't issue the same warning for Double.TryParse function.

    To repro this issue, would you mind sharing us a simple sample? Which VS version are you using? In addition, please share us a screen shot about the result (the warning message) you got in your side.

    In addition, as far as I know, the Parse method is different with TryParse method like this thread,

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 20, 2013 6:56 AM
    Moderator
  • Hi,

    I know the difference between Parse and TryParse, else I wouldn't have written this :-)

    My setup:

    • VS Ultimate 2012, version 11.0.60315.01 update 2
    • Project set to .NET 4 Client Profile
    • Code Analyzer: Microsoft All Rules

    Code:

    Class MainWindow 
    
    	Public Shared Function CA1305(input As String) As Double
    		' Here CA1305 is reported
    		Dim v1 = Double.Parse(input)
    
    		Dim v2 As Double
    		' Here CA1305 is not reported
    		If Not Double.TryParse(input, v2)
    			v2 = 0
    		End If
    
    		Return v1 + v2
    	End Function
    
    End Class
    

    Code Analyzer results:


    Mikhail

    Tuesday, May 21, 2013 7:34 AM
  • >Code Analyzer reports a potential bug, when e.g. Double.Parse is used without providing IFormatProvider, but doesn't issue the same warning for Double.TryParse function.

    If this reproduces with the latest VS2012 I suggest that you submit a
    bug report on the MS connect site:
    http://connect.microsoft.com/VisualStudio/

    Dave

    Tuesday, May 21, 2013 12:08 PM
  • Hi Mikhail,

    Double.TryParse (String, Double) Method will be different from the Double.Parse(String) method.

    http://msdn.microsoft.com/en-us/library/994c0zb1.aspx

    As my understanding, it generated this warning since sometimes it will be not safe.

    CA1305 Specify IFormatProvider               Because the behavior of 'Double.Parse(String)' could vary based on the current user's locale settings, replace this call in 'Class1.CA1305(String)' with a call to 'Double.Parse(String, IFormatProvider)'. If the result of 'Double.Parse(String, IFormatProvider)' will be based on input from the user, specify 'CultureInfo.CurrentCulture()' as the 'IFormatProvider' parameter. Otherwise, if the result will based on input stored and accessed by software, such as when it is loaded from disk or from a database, specify 'CultureInfo.InvariantCulture()'.

    Two threads about the similar issue, reference:

    CA1305: int.Parse(String)

    Int32.Parse(String, IFormatProvider) - why use?

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 22, 2013 2:41 AM
    Moderator
  • Jack, I'm afraid you misunderstood me. I don't have the slightest problem with Parse generating the warning. I have a problem with TryParse NOT generating it.

    And I find the issue reporting tool cumbersome, so I consider I've reported it here. Thanks for understanding :-)


    Mikhail


    • Edited by MikNik Wednesday, May 22, 2013 5:42 AM
    Wednesday, May 22, 2013 5:38 AM
  • Hi Mikhail,

    As my understanding, the TryParse has handled the risks/exception with Try…Catch, so it doesn’t have this issue.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 22, 2013 8:19 AM
    Moderator
  • Warning 1305 has nothing to do with exceptions.

    It tells that there is a risk of parsing a string incorrectly: the US-styled number "2.5" will be parsed as "25" in e.g. Danish settings, where the decimal separator is ",".


    Mikhail

    Wednesday, May 22, 2013 8:41 AM
  • >And I find the issue reporting tool cumbersome, so I consider I've reported it here. Thanks for understanding :-)

    The connect site is the only easy way we have to get bug reports to
    MS.

    See if this says what's needed (let me know if it doesn't and I'll
    modify it), and add your vote/validation to it:

    https://connect.microsoft.com/VisualStudio/feedback/details/788373/code-analysis-rule-ca1305-is-missed-for-tryparse#details

    Cheers
    Dave

    • Marked as answer by MikNik Wednesday, May 22, 2013 9:58 AM
    Wednesday, May 22, 2013 9:40 AM
  • Hi Mikhail,

    I doubt that TryParse uses the current culture by default. So it doesn’t have this issue.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 22, 2013 9:40 AM
    Moderator
  • What does it use then?

    Mikhail

    Wednesday, May 22, 2013 9:59 AM
  • >I doubt that TryParse uses the current culture by default. So it doesn’t have this issue.

    A quick look with reflector shows that the simple TryParse calls:

                return TryParse(s, NumberStyles.Float |
    NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out
    result);

    And a test shows that:

        double dv;
        if ( Double.TryParse( "2,500", out dv ) )

    Results in 2500.0 when the decimal symbol is '.', and 2.5 when it's
    ','.

    I don't see any reason not to get CA1305 with TryParse.

    Dave

    Wednesday, May 22, 2013 10:44 AM
  • Tuesday, October 9, 2018 1:31 PM