Skip to content

Instantly share code, notes, and snippets.

@sangelov
Created February 6, 2015 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sangelov/1b75375a8fc2854842dd to your computer and use it in GitHub Desktop.
Save sangelov/1b75375a8fc2854842dd to your computer and use it in GitHub Desktop.
A JustCode extension that adds a warning for < > <= >= between double/float
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