Created
February 6, 2015 17:20
-
-
Save sangelov/1b75375a8fc2854842dd to your computer and use it in GitHub Desktop.
A JustCode extension that adds a warning for < > <= >= between double/float
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.ComponentModel.Composition; | |
using Telerik.JustCode.CommonLanguageModel; | |
namespace JustCodeExtension3 | |
{ | |
[Export(typeof(IEngineModule))] | |
[Export(typeof(ICodeMarkerGroupDefinition))] | |
public class WarningExample : CodeMarkerProviderModuleBase | |
{ | |
private const string WarningExampleID = "JustCode_FloatingPointComparison"; | |
private const string MarkerText = "Using direct comparison between floating point values might be dangerous"; | |
private const string Description = "Warning for usage of direct comparison between floating point values"; | |
public override IEnumerable<CodeMarkerGroup> CodeMarkerGroups | |
{ | |
get | |
{ | |
foreach (var language in new[] { LanguageNames.CSharp, LanguageNames.VisualBasic }) | |
{ | |
yield return CodeMarkerGroup.Define( | |
language, | |
WarningExampleID, | |
CodeMarkerAppearance.Warning, | |
Description, | |
true, | |
MarkerText, | |
string.Empty); | |
} | |
} | |
} | |
protected override void AddCodeMarkers(FileModel fileModel) | |
{ | |
foreach (var expression in fileModel.All<IBinaryExpression>().Where(x => x.Operator == Operator.Less || x.Operator == Operator.LessOrEqual || | |
x.Operator == Operator.Greater || x.Operator == Operator.GreaterOrEqual)) | |
{ | |
var leftExpressionIsFloatingPoint = expression.LeftExpression.Type.Is("System.Single") || expression.LeftExpression.Type.Is("System.Double"); | |
var rightExpressionIsFloatingPoint = expression.RightExpression.Type.Is("System.Single") || expression.RightExpression.Type.Is("System.Double"); | |
if (leftExpressionIsFloatingPoint && rightExpressionIsFloatingPoint) | |
{ | |
expression.AddCodeMarker(WarningExampleID, this); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment