• D
    Fix VB incorrectly reported redundant casts in compound assignment statements · fa0e0b07
    Dustin Campbell 提交于
    Consider the following code:
    
    ```VB
    Dim b As Byte
    b += CByte(1)
    ```
    
    The `CByte` cast can be removed if the Option Strict is Off. However, if Option Strict is On, the compiler
    produces an error ("Option Strict On disallows implicit conversions from 'Integer' to 'Byte'").
    
    The issue here is that compound statements are not considered by unnecessary cast detection. Unfortunately,
    supporting is not as straight forward as I'd hoped for a couple of reasons:
    
    1. There is no compiler API to get the built-in operator used by a VB assignment statement. If there were, this
    would be a straightforward fix since removing the cast results in the compiler choosing `System.Int32.op_Add(Int32, Int32)`
    rather than `System.Byte.op_Add(Byte, Byte)`.
    
    2. There is a hidden narrowing-numeric conversion of a constant value in the following code:
    
    ```VB
    b += 1
    ```
    
    Using the compiler API to classify conversion from the expression `1` to the type of `b` results in a
    widening-numeric conversion, which is misleading since the type of `1` is `System.Int32` and the type of `b`
    is `System.Byte`.
    
    Fixing the problem requires two changes:
    
    1. Actually support assignment statements in the speculative analyzer and check to see if replacment would break
    the compound assignment.
    2. Add a special case in the VB speculative analyzer in the case that Option Strict is not Off and the expression
    has a constant value. In that case, we use a different compiler API to classify *the type of the expression* `1` to
    the type of `b`, rather than classifying *the expression* `1` to the type of `b`.
    fa0e0b07
CastSimplificationTests.vb 172.9 KB