<commit_before>﻿// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.ReadabilityRules
{
    using System.Collections.Immutable;
    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.CSharp;
    using Microsoft.CodeAnalysis.CSharp.Syntax;
    using Microsoft.CodeAnalysis.Diagnostics;

    /// <summary>
    /// A value type was constructed using the syntax <c>new T()</c>.
    /// </summary>
    [DiagnosticAnalyzer(LanguageNames.CSharp)]
    internal class SA1129DoNotUseDefaultValueTypeConstructor : DiagnosticAnalyzer
    {
        /// <summary>
        /// The ID for diagnostics produced by the <see cref="SA1129DoNotUseDefaultValueTypeConstructor"/> analyzer.
        /// </summary>
        public const string DiagnosticId = "SA1129";
        private static readonly LocalizableString Title = new LocalizableResourceString(nameof(ReadabilityResources.SA1129Title), ReadabilityResources.ResourceManager, typeof(ReadabilityResources));
        private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(ReadabilityResources.SA1129MessageFormat), ReadabilityResources.ResourceManager, typeof(ReadabilityResources));
        private static readonly LocalizableString Description = new LocalizableResourceString(nameof(ReadabilityResources.SA1129Description), ReadabilityResources.ResourceManager, typeof(ReadabilityResources));
        private static readonly string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1129.md";

        private static readonly DiagnosticDescriptor Descriptor =
            new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.ReadabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.DisabledNoTests, Description, HelpLink);

        /// <inheritdoc/>
        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; }
            = ImmutableArray.Create(Descriptor);

        /// <inheritdoc/>
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterCompilationStartAction(HandleCompilationStart);
        }

        private static void HandleCompilationStart(CompilationStartAnalysisContext context)
        {
            context.RegisterSyntaxNodeActionHonorExclusions(HandleNewExpression, SyntaxKind.ObjectCreationExpression);
        }

        private static void HandleNewExpression(SyntaxNodeAnalysisContext context)
        {
            ObjectCreationExpressionSyntax newExpression = (ObjectCreationExpressionSyntax)context.Node;

            var typeToCreate = context.SemanticModel.GetTypeInfo(newExpression, context.CancellationToken);
            if (typeToCreate.Type.IsReferenceType || IsReferenceTypeParameter(typeToCreate.Type))
            {
                return;
            }

            if ((newExpression.ArgumentList == null) || (newExpression.ArgumentList.Arguments.Count > 0))
            {
                return;
            }

            if (newExpression.Initializer != null)
            {
                return;
            }

            context.ReportDiagnostic(Diagnostic.Create(Descriptor, newExpression.GetLocation()));
        }

        private static bool IsReferenceTypeParameter(ITypeSymbol type)
        {
            return (type.Kind == SymbolKind.TypeParameter) && !((ITypeParameterSymbol)type).HasValueTypeConstraint;
        }
    }
}
<commit_msg>Enable SA1129 (DoNotUseDefaultValueTypeConstructor) by default<commit_after>﻿// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.ReadabilityRules
{
    using System.Collections.Immutable;
    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.CSharp;
    using Microsoft.CodeAnalysis.CSharp.Syntax;
    using Microsoft.CodeAnalysis.Diagnostics;

    /// <summary>
    /// A value type was constructed using the syntax <c>new T()</c>.
    /// </summary>
    [DiagnosticAnalyzer(LanguageNames.CSharp)]
    internal class SA1129DoNotUseDefaultValueTypeConstructor : DiagnosticAnalyzer
    {
        /// <summary>
        /// The ID for diagnostics produced by the <see cref="SA1129DoNotUseDefaultValueTypeConstructor"/> analyzer.
        /// </summary>
        public const string DiagnosticId = "SA1129";
        private static readonly LocalizableString Title = new LocalizableResourceString(nameof(ReadabilityResources.SA1129Title), ReadabilityResources.ResourceManager, typeof(ReadabilityResources));
        private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(ReadabilityResources.SA1129MessageFormat), ReadabilityResources.ResourceManager, typeof(ReadabilityResources));
        private static readonly LocalizableString Description = new LocalizableResourceString(nameof(ReadabilityResources.SA1129Description), ReadabilityResources.ResourceManager, typeof(ReadabilityResources));
        private static readonly string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1129.md";

        private static readonly DiagnosticDescriptor Descriptor =
            new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.ReadabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);

        /// <inheritdoc/>
        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; }
            = ImmutableArray.Create(Descriptor);

        /// <inheritdoc/>
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterCompilationStartAction(HandleCompilationStart);
        }

        private static void HandleCompilationStart(CompilationStartAnalysisContext context)
        {
            context.RegisterSyntaxNodeActionHonorExclusions(HandleNewExpression, SyntaxKind.ObjectCreationExpression);
        }

        private static void HandleNewExpression(SyntaxNodeAnalysisContext context)
        {
            ObjectCreationExpressionSyntax newExpression = (ObjectCreationExpressionSyntax)context.Node;

            var typeToCreate = context.SemanticModel.GetTypeInfo(newExpression, context.CancellationToken);
            if (typeToCreate.Type.IsReferenceType || IsReferenceTypeParameter(typeToCreate.Type))
            {
                return;
            }

            if ((newExpression.ArgumentList == null) || (newExpression.ArgumentList.Arguments.Count > 0))
            {
                return;
            }

            if (newExpression.Initializer != null)
            {
                return;
            }

            context.ReportDiagnostic(Diagnostic.Create(Descriptor, newExpression.GetLocation()));
        }

        private static bool IsReferenceTypeParameter(ITypeSymbol type)
        {
            return (type.Kind == SymbolKind.TypeParameter) && !((ITypeParameterSymbol)type).HasValueTypeConstraint;
        }
    }
}

{'fraction_non_alphanumeric': 0.0781163434903047, 'fraction_numerical': 0.009141274238227148, 'mean_word_length': 3.3790175864160097, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1379143', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1642, 'n_words': 358}