Giter VIP home page Giter VIP logo

codeanalyzer's People

Contributors

jnyrup avatar kthaulow avatar

Watchers

 avatar  avatar

codeanalyzer's Issues

Static readonly

public static skal være readonly (note: public static readonly valueType kan være const)

double InvariantCulture not detected

The follwoing code is marked as not using InvariantCulture:

double.TryParse(lon.ExceptLast(1), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture.NumberFormat, out longitude)

Orderby

a = a.orderBy(pred).tolist() => a.sort(pred)

OrderBy().FirstOrDefault() => MinByOrDefault

float analyzers

The analyzers for double e.g. Parse and TryParse using InvariantCulture should also apply to float.

TimeSpan format checks

When formatting a TimeSpan using one the following is always invalid:

Year and month does not apply TimeSpan

  • y
  • Y
  • yy
  • yyy
  • yyyy
  • M "Did you mean mm (minutes)"
  • MM "Did you mean mm (minutes)"
  • MMM
  • MMMM

TimeSpan doesn't operator with 12 vs 24h clock, so this is invalid:

  • HH "Did you mean hh (hours)"

  • D "Did you mean dd (days)"

  • DD "Did you mean dd (days)"

Special cases:

  • d alone doesn't work, use %d or in a pattern @"d\:h"
  • h alone doesn't work, use %h or in a pattern @"h\:m"
  • m alone doesn't work, use %d or in a pattern @"m\:s"
  • s alone doesn't work, use %s or in a pattern @"s\:f"
  • f alone doesn't work, use %f or in a pattern @"f\:ff"
  • F alone doesn't work, use %F or in a pattern @"F\:ff"

ThreadUnsafe Attribute

Assume we have a custom attribute e.g. [ThreadUnsafe] for methods and properties.

Then for unit tests, calling functions/fields/classes decorated with [ThreadUnsafe] is only allowed, if used in:

  • [TestMethod] and method is decorated with [DoNotParallelize], or
  • [ClassInitialize] and class is decorated with [DoNotParallelize], or
  • [AssemblyInitialize].

False positive circuit breaker

AN0001 is triggered in this code

`int added = 0;
int loopCount = 0;
while (added< 10 && list.Any())
{
foreach (var sourceGroup in sourceGroups)
{
;
}
}
loopCount++;
if (loopCount > 100)
break;

            }`

Log name

Logger: typen skal følge className
e.g. ACLogManager.Instance.GetLogger for klasssen NavigationRepository

DateTime Now -> UtcNow

DateTime.Now 
	=> DateTimeExt.Now
	
DateTime.Today 
	=> DateTimeExt.UtcToday
	
DateTime.{Parse, ParseExact, TryParse, TryParseExact} 
	=> DateTimeExt.{Parse, ParseExact, TryParse, TryParseExact}
	
new DateTime(year, month, day) 
	=> DateTimeExt.UtcDate(year, month, day)
	
new DateTime(year, month, day, minute, hour, second) 
	=> DateTimeExt.UtcDate(year, month, day, hour, minute, second)
	
new DateTime(year, month, day, hour, minute, 0) 
	=> DateTimeExt.UtcDate(year, month, day, hour, minute)
	
new DateTime(year, month, day, hour, 0, 0) 
	=> DateTimeExt.UtcDate(year, month, day, hour)
	
new DateTime(year, month, day, 0, 0, 0) 
	=> DateTimeExt.UtcDate(year, month, day)

Use 24 hour format

When using a datetime format, check that 24 hours format is used. "HH" should be used instead of "hh".

DateTime format checks

The following patterns (with any separator) should probably have MM (month) instead of m (minutes)
when formatting a DateTime or DateTimeOffset.

  • yyyy/mm/dd
  • mm/dd/yyyy
  • dd/mm/yyyy
  • yy/mm/dd
  • mm/dd/yy
  • dd/mm/yy

Lock

private xxxLock = new object() => private [static] readonly xxxLock = new object()

Singleton pattern

Singleton-pattern:
ACLogger s_log => private static readonly
dobbelt null check
private ctor
sealed class

Exception in AnalyzeInvocationExpressionSyntax

TimeFormatAnalyzer

Severity	Code	Description	Project	File	Line	Suppression State	Detail Description
Warning	AD0001	Analyzer 'CodeAnalyzer.Analyzers.TimeFormatAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.	ConsoleApp44		1	Active	Analyzer 'CodeAnalyzer.Analyzers.TimeFormatAnalyzer' threw the following exception:
'Exception occurred with following context:
Compilation: ConsoleApp44
SyntaxTree: C:\Users\jonas\source\repos\ConsoleApp44\ConsoleApp44\Program.cs
SyntaxNode: F(Date) [InvocationExpressionSyntax]@[505..512) (18,12)-(18,19)

System.NullReferenceException: Object reference not set to an instance of an object.
   at CodeAnalyzer.Analyzers.TimeFormatAnalyzer.AnalyzeInvocationExpressionSyntax(SyntaxNodeAnalysisContext context, INamedTypeSymbol dateTimeSymbol)
   at CodeAnalyzer.Analyzers.TimeFormatAnalyzer.<>c__DisplayClass8_0.<Initialize>b__1(SyntaxNodeAnalysisContext nodeContext)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__44`1.<ExecuteSyntaxNodeAction>b__44_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----
'.

DoubleParseAnalyzer

Severity	Code	Description	Project	File	Line	Suppression State	Detail Description
Warning	AD0001	Analyzer 'CodeAnalyzer.Analyzers.DoubleParseAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.	ConsoleApp44		1	Active	Analyzer 'CodeAnalyzer.Analyzers.DoubleParseAnalyzer' threw the following exception:
'Exception occurred with following context:
Compilation: ConsoleApp44
SyntaxTree: C:\Users\jonas\source\repos\ConsoleApp44\ConsoleApp44\Program.cs
SyntaxNode: F(Date) [InvocationExpressionSyntax]@[505..512) (18,12)-(18,19)

System.NullReferenceException: Object reference not set to an instance of an object.
   at CodeAnalyzer.Analyzers.DoubleParseAnalyzer.AnalyzeInvocationExpressionSyntax(SyntaxNodeAnalysisContext context, INamedTypeSymbol doubleSymbol)
   at CodeAnalyzer.Analyzers.DoubleParseAnalyzer.<>c__DisplayClass8_0.<Initialize>b__1(SyntaxNodeAnalysisContext nodeContext)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__44`1.<ExecuteSyntaxNodeAction>b__44_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----
'.

DateTimeStringAnalyzer

Severity	Code	Description	Project	File	Line	Suppression State	Detail Description
Warning	AD0001	Analyzer 'CodeAnalyzer.Analyzers.DateTimeStringAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.	ConsoleApp44		1	Active	Analyzer 'CodeAnalyzer.Analyzers.DateTimeStringAnalyzer' threw the following exception:
'Exception occurred with following context:
Compilation: ConsoleApp44
SyntaxTree: C:\Users\jonas\source\repos\ConsoleApp44\ConsoleApp44\Program.cs
SyntaxNode: F(Date) [InvocationExpressionSyntax]@[505..512) (18,12)-(18,19)

System.NullReferenceException: Object reference not set to an instance of an object.
   at CodeAnalyzer.Analyzers.DateTimeStringAnalyzer.AnalyzeInvocationExpressionSyntax(SyntaxNodeAnalysisContext context, INamedTypeSymbol doubleSymbol)
   at CodeAnalyzer.Analyzers.DateTimeStringAnalyzer.<>c__DisplayClass8_0.<Initialize>b__1(SyntaxNodeAnalysisContext nodeContext)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__44`1.<ExecuteSyntaxNodeAction>b__44_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----
'.

SqlDataReader loop

AN0001 should not detect loops without circuit breaker for SqlDataReader:

while (reader.Read()) {}

Circuit breaker in While condition

The following circuit breaker should be supported

int loopCounter = 0; int something1 = 0; int something2 = 1; while (something1 != something2 && loopCounter < 10) { loopCounter++; }

TimeSpan escaped character

TimeSpan stayTime;

stayTime.ToString(@"\Dh+mm", CultureInfo.InvariantCulture);

This should not be marked by AN0009

Remove

X.Remove(X.LastOrDefault) => X.RemoveLast()
Det første fjerner første element identisk med X.Last(), så hvis der er flere identiske vil den første blive fjernet i stedet for den sidste

Unit test naming

Show info about code style for methods having [TestMethod] attribute if the method name is not on the form XXX_YYY_ZZZ. This should represent [Unit to be tested][Scenario][Expected outcome]

Assert in unit test

Check that Assert or Should() (from FluentAssertions) are being called in every unit test. Check private method calls as the assert could be in a method called.

Any cast

Linq.Any(e => e == b.ToX()) (ingen grund til at kalde b.ToX() for hver sammenligning)
	=> var bb = b.ToX(); Linq.Any(e => bb)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.