Skip to content

Commit

Permalink
Remove boxing from Empty/NotEmpty validators.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremySkinner committed Nov 18, 2022
1 parent 84462a6 commit beb8f60
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 14 deletions.
9 changes: 2 additions & 7 deletions src/FluentValidation/Validators/EmptyValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace FluentValidation.Validators;

using System;
using System.Collections;
using System.Collections.Generic;
using Resources;

public class EmptyValidator<T,TProperty> : PropertyValidator<T,TProperty> {
Expand All @@ -38,13 +39,7 @@ public class EmptyValidator<T,TProperty> : PropertyValidator<T,TProperty> {
return true;
}

//TODO: Rewrite to avoid boxing
if (Equals(value, default(TProperty))) {
// Note: Code analysis indicates "Expression is always false" but this is incorrect.
return true;
}

return false;
return EqualityComparer<TProperty>.Default.Equals(value, default);
}

protected override string GetDefaultMessageTemplate(string errorCode) {
Expand Down
9 changes: 2 additions & 7 deletions src/FluentValidation/Validators/NotEmptyValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace FluentValidation.Validators;

using System;
using System.Collections;
using System.Collections.Generic;

public class NotEmptyValidator<T,TProperty> : PropertyValidator<T, TProperty>, INotEmptyValidator {

Expand All @@ -37,13 +38,7 @@ public class NotEmptyValidator<T,TProperty> : PropertyValidator<T, TProperty>, I
return false;
}

//TODO: Rewrite to avoid boxing
if (Equals(value, default(TProperty))) {
// Note: Code analysis indicates "Expression is always false" but this is incorrect.
return false;
}

return true;
return !EqualityComparer<TProperty>.Default.Equals(value, default);
}

protected override string GetDefaultMessageTemplate(string errorCode) {
Expand Down

0 comments on commit beb8f60

Please sign in to comment.