Skip to content

Commit ff9f3af

Browse files
committed
added missing guard clauses
upgraded to latest version of LiteGuard.Source
1 parent b3d1fbd commit ff9f3af

4 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/ScriptCs.Core/FilePreProcessor.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public virtual FilePreProcessorResult ProcessScript(string script)
3333

3434
protected virtual FilePreProcessorResult Parse(Action<FilePreProcessorContext> parseAction)
3535
{
36+
Guard.AgainstNullArgument("parseAction", parseAction);
37+
3638
var context = new FilePreProcessorContext();
3739

3840
_logger.DebugFormat("Starting pre-processing");
@@ -54,6 +56,8 @@ protected virtual FilePreProcessorResult Parse(Action<FilePreProcessorContext> p
5456

5557
protected virtual string GenerateCode(FilePreProcessorContext context)
5658
{
59+
Guard.AgainstNullArgument("context", context);
60+
5761
var stringBuilder = new StringBuilder();
5862

5963
var usingLines = context.UsingStatements
@@ -82,6 +86,9 @@ protected virtual void ParseFile(string path, FilePreProcessorContext context)
8286

8387
protected virtual void ParseScript(List<string> scriptLines, FilePreProcessorContext context, string path = null)
8488
{
89+
Guard.AgainstNullArgument("scriptLines", scriptLines);
90+
Guard.AgainstNullArgument("context", context);
91+
8592
// Insert line directive if there's a path
8693
if (path != null) InsertLineDirective(path, scriptLines);
8794

@@ -97,6 +104,8 @@ protected virtual void ParseScript(List<string> scriptLines, FilePreProcessorCon
97104

98105
protected virtual void InsertLineDirective(string path, List<string> fileLines)
99106
{
107+
Guard.AgainstNullArgument("fileLines", fileLines);
108+
100109
var bodyIndex = fileLines.FindIndex(line => PreProcessorUtil.IsNonDirectiveLine(line) && !PreProcessorUtil.IsUsingLine(line));
101110
if (bodyIndex == -1) return;
102111

@@ -106,6 +115,8 @@ protected virtual void InsertLineDirective(string path, List<string> fileLines)
106115

107116
protected virtual void ProcessLine(FilePreProcessorContext context, string line, bool isBeforeCode)
108117
{
118+
Guard.AgainstNullArgument("context", context);
119+
109120
if (PreProcessorUtil.IsUsingLine(line))
110121
{
111122
var @using = PreProcessorUtil.GetPath(PreProcessorUtil.UsingString, line);

src/ScriptCs.Core/FileSystem.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public void FileDelete(string path)
7878

7979
public IEnumerable<string> SplitLines(string value)
8080
{
81+
Guard.AgainstNullArgument("value", value);
82+
8183
return value.Split(new[] { NewLine }, StringSplitOptions.None);
8284
}
8385

src/ScriptCs.Core/Guard.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace ScriptCs
22
{
33
using System;
44
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Globalization;
67

78
/// <summary>
@@ -17,6 +18,7 @@ internal static class Guard
1718
/// <param name="argument">The argument.</param>
1819
/// <exception cref="System.ArgumentNullException"><paramref name="argument" /> is <c>null</c>.</exception>
1920
/// <remarks><typeparamref name="TArgument"/> is restricted to reference types to avoid boxing of value type objects.</remarks>
21+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Distributed as a source code package.")]
2022
[DebuggerStepThrough]
2123
public static void AgainstNullArgument<TArgument>(string parameterName, [ValidatedNotNull]TArgument argument) where TArgument : class
2224
{
@@ -36,6 +38,7 @@ public static void AgainstNullArgument<TArgument>(string parameterName, [Validat
3638
/// <remarks>
3739
/// Performs a type check to avoid boxing of value type objects.
3840
/// </remarks>
41+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Distributed as a source code package.")]
3942
[DebuggerStepThrough]
4043
public static void AgainstNullArgumentIfNullable<TArgument>(string parameterName, [ValidatedNotNull]TArgument argument)
4144
{
@@ -54,6 +57,7 @@ public static void AgainstNullArgumentIfNullable<TArgument>(string parameterName
5457
/// <param name="argumentProperty">The argument property.</param>
5558
/// <exception cref="System.ArgumentException"><paramref name="argumentProperty" /> is <c>null</c>.</exception>
5659
/// <remarks><typeparamref name="TProperty"/> is restricted to reference types to avoid boxing of value type objects.</remarks>
60+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Distributed as a source code package.")]
5761
[DebuggerStepThrough]
5862
public static void AgainstNullArgumentProperty<TProperty>(string parameterName, string propertyName, [ValidatedNotNull]TProperty argumentProperty)
5963
where TProperty : class
@@ -75,6 +79,7 @@ public static void AgainstNullArgumentProperty<TProperty>(string parameterName,
7579
/// <remarks>
7680
/// Performs a type check to avoid boxing of value type objects.
7781
/// </remarks>
82+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Distributed as a source code package.")]
7883
[DebuggerStepThrough]
7984
public static void AgainstNullArgumentPropertyIfNullable<TProperty>(
8085
string parameterName, string propertyName, [ValidatedNotNull]TProperty argumentProperty)
@@ -85,12 +90,22 @@ public static void AgainstNullArgumentPropertyIfNullable<TProperty>(
8590
}
8691
}
8792

93+
/// <summary>
94+
/// Determines whether the specified type is a nullable type.
95+
/// </summary>
96+
/// <param name="type">The type.</param>
97+
/// <returns>
98+
/// <c>true</c> if the specified type is a nullable type; otherwise, <c>false</c>.
99+
/// </returns>
100+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Distributed as a source code package.")]
88101
private static bool IsNullableType(this Type type)
89102
{
90103
return !type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>));
91104
}
92105

93-
// NOTE: when applied to a parameter, this attribute provides an indication to code analysis that the argument has been null checked
106+
/// <summary>
107+
/// When applied to a parameter, this attribute provides an indication to code analysis that the argument has been null checked.
108+
/// </summary>
94109
private sealed class ValidatedNotNullAttribute : Attribute
95110
{
96111
}

src/ScriptCs/ScriptCsArgs.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public class ScriptCsArgs
5252

5353
public static void SplitScriptArgs(ref string[] args, out string[] scriptArgs)
5454
{
55+
Guard.AgainstNullArgument("args", args);
56+
5557
// Split the arguments list on "--".
5658
// The arguments before the "--" (or all arguments if there is no "--") are
5759
// for ScriptCs.exe, and the arguments after that are for the csx script.

0 commit comments

Comments
 (0)