Esempio n. 1
0
        /*********
        ** Private methods
        *********/
        /// <summary>Analyse a syntax node and add a diagnostic message if it references an obsolete field.</summary>
        /// <param name="context">The analysis context.</param>
        private void AnalyzeObsoleteFields(SyntaxNodeAnalysisContext context)
        {
            try
            {
                // get reference info
                if (!AnalyzerUtilities.TryGetMemberInfo(context.Node, context.SemanticModel, out ITypeSymbol declaringType, out TypeInfo memberType, out string memberName))
                {
                    return;
                }

                // suggest replacement
                foreach (ITypeSymbol type in AnalyzerUtilities.GetConcreteTypes(declaringType))
                {
                    if (this.ReplacedFields.TryGetValue($"{type}::{memberName}", out string replacement))
                    {
                        context.ReportDiagnostic(Diagnostic.Create(this.Rules["AvoidObsoleteField"], context.Node.GetLocation(), $"{type}.{memberName}", replacement));
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Failed processing expression: '{context.Node}'. Exception details: {ex.ToString().Replace('\r', ' ').Replace('\n', ' ')}");
            }
        }
Esempio n. 2
0
        /*********
        ** Private methods
        *********/
        /// <summary>Analyze a member access syntax node and add a diagnostic message if applicable.</summary>
        /// <param name="context">The analysis context.</param>
        /// <returns>Returns whether any warnings were added.</returns>
        private void AnalyzeMemberAccess(SyntaxNodeAnalysisContext context)
        {
            this.HandleErrors(context.Node, () =>
            {
                // get member access info
                if (!AnalyzerUtilities.TryGetMemberInfo(context.Node, context.SemanticModel, out ITypeSymbol declaringType, out TypeInfo memberType, out string memberName))
                {
                    return;
                }
                if (!this.IsNetType(memberType.Type))
                {
                    return;
                }

                // warn: use property wrapper if available
                foreach (ITypeSymbol type in AnalyzerUtilities.GetConcreteTypes(declaringType))
                {
                    if (this.NetFieldWrapperProperties.TryGetValue($"{type}::{memberName}", out string suggestedPropertyName))
                    {
                        context.ReportDiagnostic(Diagnostic.Create(this.AvoidNetFieldRule, context.Node.GetLocation(), context.Node, memberType.Type.Name, suggestedPropertyName));
                        return;
                    }
                }

                // warn: implicit conversion
                if (this.IsInvalidConversion(memberType.Type, memberType.ConvertedType))
                {
                    context.ReportDiagnostic(Diagnostic.Create(this.AvoidImplicitNetFieldCastRule, context.Node.GetLocation(), context.Node, memberType.Type.Name, memberType.ConvertedType));
                    return;
                }
            });