Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CallExtensionMethodAsExtensionAnalyzer: ArgumentException #1018

Open
ozonni opened this issue Feb 15, 2019 · 4 comments
Open

CallExtensionMethodAsExtensionAnalyzer: ArgumentException #1018

ozonni opened this issue Feb 15, 2019 · 4 comments

Comments

@ozonni
Copy link

ozonni commented Feb 15, 2019

Analyzer 'CodeCracker.CSharp.Usage.CallExtensionMethodAsExtensionAnalyzer' threw an exception of type 'System.ArgumentException' with message 'Inconsistent syntax tree features

Parameter name: trees'.

Started after we migrated to .net core 2.2. Have no idea where exactly it happens

@HenningNT
Copy link

I see this in my build pipeline, when running the tests. Which is odd, since the test project doesn't include CodeCracker...

@pgrm
Copy link

pgrm commented Apr 9, 2020

Hello, I just had the same exception. The problem was

CustomAttributeExtensions.GetCustomAttributes(type, inherit: true);

probably because changing it to type.GetCustomAttributes(true) - would have called a different method and not the extension method.

@bddckr
Copy link

bddckr commented Apr 15, 2020

I just got the same issue! The first two extension methods here trigger it:

namespace Something
{
    using System;
    using System.Management;

    /// <summary>Extension methods for <see cref="ManagementBaseObject"/>.</summary>
    internal static class ManagementBaseObjectExtensions
    {
        /// <summary>Tries to get the value of a property.</summary>
        /// <typeparam name="TProperty">The type of the value to retrieve.</typeparam>
        /// <param name="managementObject">The object of which to retrieve the value from.</param>
        /// <param name="propertyName">The name of the property to retrieve.</param>
        /// <returns>The property value, if it exists, otherwise <see langword="null"/>.</returns>
        public static TProperty? TryGetPropertyValue<TProperty>(this ManagementBaseObject managementObject, string propertyName)
            where TProperty : class
        {
            try
            {
                return (TProperty?)managementObject.Properties[propertyName]?.Value;
            }
            catch (ManagementException)
            {
                return null;
            }
        }

        /// <summary>Tries to get the value of a property.</summary>
        /// <typeparam name="TProperty">The type of the value to retrieve.</typeparam>
        /// <param name="managementObject">The object of which to retrieve the value from.</param>
        /// <param name="propertyName">The name of the property to retrieve.</param>
        /// <returns>The property value, if it exists, otherwise <see langword="null"/>.</returns>
        public static TProperty? TryGetPropertyStructValue<TProperty>(this ManagementBaseObject managementObject, string propertyName)
            where TProperty : struct
        {
            try
            {
                return (TProperty?)managementObject.Properties[propertyName]?.Value;
            }
            catch (ManagementException)
            {
                return null;
            }
        }

        /// <summary>Tries to get the <see cref="DateTime"/> value of a property.</summary>
        /// <param name="managementObject">The object of which to retrieve the value from.</param>
        /// <param name="propertyName">The name of the property to retrieve.</param>
        /// <returns>The property value, if it exists, otherwise <see langword="null"/>.</returns>
        public static DateTime? TryGetDateTime(this ManagementBaseObject managementObject, string propertyName)
        {
            var dmtfDate = managementObject.TryGetPropertyValue<string>(propertyName);
            return dmtfDate == null ? (DateTime?)null : ManagementDateTimeConverter.ToDateTime(dmtfDate);
        }
    }
}

That is with .NET Standard 2.1, nullable reference types enabled.

I think in this case it's due to the generic constraint and/or the nullable reference type?

@WinniX
Copy link

WinniX commented Mar 25, 2022

Got the same one and the problem was with method Enumerable.SequenceEqual().

Fails:

Enumerable.SequenceEqual(first.OrderBy(x => x), second.OrderBy(x => x))

Works:

first.OrderBy(x => x).SequenceEqual(second.OrderBy(x => x))

Funny that both versions works, but Enumerable.SequenceEqual() succeeds only after the second try. Turned out that SequenceEqual is an extension method, that's why the analyzer doesn't like when it's not used as supposed to be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants