Skip to content

Commit

Permalink
Static analysis: use range indexer and index from end
Browse files Browse the repository at this point in the history
Part of #26805
  • Loading branch information
ajcvickers committed Dec 6, 2021
1 parent 57d1ec9 commit 7d6a411
Show file tree
Hide file tree
Showing 22 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/EFCore.Design/Design/Internal/CSharpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public virtual string Identifier(string name, ICollection<string>? scope = null,

if (partStart != name.Length)
{
builder.Append(name.Substring(partStart));
builder.Append(name[partStart..]);
}

if (builder.Length == 0
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Design/Design/Internal/DatabaseOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public virtual SavedModelFiles ScaffoldContext(
return null;
}

var subPath = outputDir.Substring(projectDir.Length);
var subPath = outputDir[projectDir.Length..];

return !string.IsNullOrWhiteSpace(subPath)
? string.Join(
Expand All @@ -151,7 +151,7 @@ private static string NormalizeDir(string path)
return path;
}

var last = path[path.Length - 1];
var last = path[^1];
return last == Path.DirectorySeparatorChar
|| last == Path.AltDirectorySeparatorChar
? path
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public virtual void Optimize(string? outputDir, string? modelNamespace, string?
return null;
}

var subPath = outputDir.Substring(projectDir.Length);
var subPath = outputDir[projectDir.Length..];

return !string.IsNullOrWhiteSpace(subPath)
? string.Join(
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Design/Design/Internal/MigrationsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public virtual MigrationFiles AddMigration(
return null;
}

var subPath = outputDir.Substring(_projectDir.Length);
var subPath = outputDir[_projectDir.Length..];

return !string.IsNullOrWhiteSpace(subPath)
? string.Join(
Expand Down
10 changes: 5 additions & 5 deletions src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public virtual ScaffoldedMigration ScaffoldMigration(
var genericMarkIndex = sanitizedContextName.IndexOf('`');
if (genericMarkIndex != -1)
{
sanitizedContextName = sanitizedContextName.Substring(0, genericMarkIndex);
sanitizedContextName = sanitizedContextName[..genericMarkIndex];
}

if (ContainsForeignMigrations(migrationNamespace!))
Expand Down Expand Up @@ -207,7 +207,7 @@ protected virtual string GetSubNamespace(string? rootNamespace, string @namespac
return @namespace == rootNamespace
? string.Empty
: @namespace.StartsWith(rootNamespace + '.', StringComparison.Ordinal)
? @namespace.Substring(rootNamespace.Length + 1)
? @namespace[(rootNamespace.Length + 1)..]
: @namespace;
}

Expand Down Expand Up @@ -252,7 +252,7 @@ public virtual MigrationFiles RemoveMigration(
.ToList();
if (migrations.Count != 0)
{
var migration = migrations[migrations.Count - 1];
var migration = migrations[^1];
model = Dependencies.SnapshotModelProcessor.Process(migration.TargetModel);

if (!Dependencies.MigrationsModelDiffer.HasDifferences(
Expand All @@ -277,7 +277,7 @@ public virtual MigrationFiles RemoveMigration(
{
Dependencies.Migrator.Migrate(
migrations.Count > 1
? migrations[migrations.Count - 2].GetId()
? migrations[^2].GetId()
: Migration.InitialDatabase);
}
else
Expand Down Expand Up @@ -314,7 +314,7 @@ public virtual MigrationFiles RemoveMigration(
}

model = migrations.Count > 1
? Dependencies.SnapshotModelProcessor.Process(migrations[migrations.Count - 2].TargetModel)
? Dependencies.SnapshotModelProcessor.Process(migrations[^2].TargetModel)
: null;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public SnapshotModelProcessor(
typeof(RelationalAnnotationNames)
.GetRuntimeFields()
.Where(p => p.Name != nameof(RelationalAnnotationNames.Prefix))
.Select(p => ((string)p.GetValue(null)!).Substring(RelationalAnnotationNames.Prefix.Length - 1)));
.Select(p => ((string)p.GetValue(null)!)[(RelationalAnnotationNames.Prefix.Length - 1)..]));
_modelRuntimeInitializer = modelRuntimeInitializer;
}

Expand Down Expand Up @@ -107,7 +107,7 @@ private void ProcessElement(IReadOnlyAnnotatable? metadata, string version)
var colon = annotation.Name.IndexOf(':');
if (colon > 0)
{
var stripped = annotation.Name.Substring(colon);
var stripped = annotation.Name[colon..];
if (_relationalNames.Contains(stripped))
{
mutableMetadata.RemoveAnnotation(annotation.Name);
Expand All @@ -121,7 +121,7 @@ private void ProcessElement(IReadOnlyAnnotatable? metadata, string version)
else if (!Equals(duplicate.Value, annotation.Value))
{
_operationReporter.WriteWarning(
DesignStrings.MultipleAnnotationConflict(stripped.Substring(1)));
DesignStrings.MultipleAnnotationConflict(stripped[1..]));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Design/Scaffolding/Internal/CSharpUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public virtual string GenerateCSharpIdentifier(
{
var proposedIdentifier =
identifier.Length > 1 && identifier[0] == '@'
? "@" + _invalidCharsRegex.Replace(identifier.Substring(1), "_")
? "@" + _invalidCharsRegex.Replace(identifier[1..], "_")
: _invalidCharsRegex.Replace(identifier, "_");
if (string.IsNullOrEmpty(proposedIdentifier))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ private static string FindCommonPrefix(string firstName, IEnumerable<string> pro
if (s.Length <= prefixLength
|| s[prefixLength] != c)
{
return firstName.Substring(0, prefixLength);
return firstName[..prefixLength];
}
}

prefixLength++;
}

return firstName.Substring(0, prefixLength);
return firstName[..prefixLength];
}

private static string StripId(string commonPrefix)
Expand All @@ -156,7 +156,7 @@ private static string StripId(string commonPrefix)
}

return i != 0
? commonPrefix.Substring(0, i + 1)
? commonPrefix[..(i + 1)]
: commonPrefix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public virtual void Intercept(IInvocation invocation)
if (_loader != null
&& methodName.StartsWith("get_", StringComparison.Ordinal))
{
var navigationName = methodName.Substring(4);
var navigationName = methodName[4..];
var navigationBase = _entityType.FindNavigation(navigationName)
?? (INavigationBase?)_entityType.FindSkipNavigation(navigationName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ protected virtual string FindPropertyName(IInvocation invocation)
}
}

return methodName.Substring(4);
return methodName[4..];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class MigrationsIdGenerator : IMigrationsIdGenerator
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string GetName(string id)
=> id.Substring(Format.Length + 1);
=> id[(Format.Length + 1)..];

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.EntityFrameworkCore.Query.SqlExpressions;
public sealed class TableExpression : TableExpressionBase, IClonableTableExpressionBase
{
internal TableExpression(ITableBase table)
: base(table.Name.Substring(0, 1).ToLowerInvariant())
: base(table.Name[..1].ToLowerInvariant())
{
Name = table.Name;
Schema = table.Schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TableValuedFunctionExpression : TableExpressionBase
/// <param name="arguments">The arguments of the function.</param>
public TableValuedFunctionExpression(IStoreFunction storeFunction, IReadOnlyList<SqlExpression> arguments)
: this(
storeFunction.Name.Substring(0, 1).ToLowerInvariant(),
storeFunction.Name[..1].ToLowerInvariant(),
storeFunction,
arguments)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public virtual string ResolveConnectionString(string connectionString)
return null;
}

return connectionString.Substring(0, firstEquals).Trim().Equals(
return connectionString[..firstEquals].Trim().Equals(
"name", StringComparison.OrdinalIgnoreCase)
? connectionString.Substring(firstEquals + 1).Trim()
? connectionString[(firstEquals + 1)..].Trim()
: null;
}
}
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Storage/RelationalTypeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ private static string GetBaseName(string storeType)
var openParen = storeType.IndexOf("(", StringComparison.Ordinal);
if (openParen >= 0)
{
storeType = storeType.Substring(0, openParen);
storeType = storeType[..openParen];
}

return storeType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
var openParen = storeTypeName.IndexOf("(", StringComparison.Ordinal);
if (openParen > 0)
{
var storeTypeNameBase = storeTypeName.Substring(0, openParen).Trim();
var storeTypeNameBase = storeTypeName[..openParen].Trim();
var closeParen = storeTypeName.IndexOf(")", openParen + 1, StringComparison.Ordinal);
if (closeParen > openParen)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ private static string ExpandFileName(string fileName)
dataDirectory = AppDomain.CurrentDomain.BaseDirectory;
}

fileName = Path.Combine(dataDirectory, fileName.Substring("|DataDirectory|".Length));
fileName = Path.Combine(dataDirectory, fileName["|DataDirectory|".Length..]);
}

return Path.GetFullPath(fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class TemporalTableExpression : TableExpressionBase, IClonableTa
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected TemporalTableExpression(ITableBase table)
: base(table.Name.Substring(0, 1).ToLowerInvariant())
: base(table.Name[..1].ToLowerInvariant())
{
Name = table.Name;
Schema = table.Schema;
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/Diagnostics/LoggerCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private static string ToName(Type loggerCategoryType)
var index = name.IndexOf(outerClassName, StringComparison.Ordinal);
if (index >= 0)
{
name = name.Substring(0, index) + name[(index + outerClassName.Length)..];
name = name[..index] + name[(index + outerClassName.Length)..];
}

return name;
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/Extensions/Internal/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static string GenerateParameterName(this Type type)
var sb = new StringBuilder();
var removeLowerCase = sb.Append(type.Name.Where(char.IsUpper).ToArray()).ToString();

return removeLowerCase.Length > 0 ? removeLowerCase.ToLowerInvariant() : type.Name.ToLowerInvariant().Substring(0, 1);
return removeLowerCase.Length > 0 ? removeLowerCase.ToLowerInvariant() : type.Name.ToLowerInvariant()[..1];
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/Metadata/Conventions/BackingFieldConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private static void DiscoverField(IConventionPropertyBaseBuilder conventionPrope
match = TryMatch(sortedFields, propertyName, "", "", propertyBase, null, entityClrType, propertyName);

var camelPrefix = char.ToLowerInvariant(propertyName[0]).ToString();
var camelizedSuffix = propertyName.Substring(1);
var camelizedSuffix = propertyName[1..];

match = TryMatch(sortedFields, camelPrefix, camelizedSuffix, "", propertyBase, match, entityClrType, propertyName);
match = TryMatch(sortedFields, "_", camelPrefix, camelizedSuffix, propertyBase, match, entityClrType, propertyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class PropertyParameterBindingFactory : IPropertyParameterBindingFactory

private static IList<string> GetCandidatePropertyNames(string parameterName)
{
var pascalized = char.ToUpperInvariant(parameterName[0]) + parameterName.Substring(1);
var pascalized = char.ToUpperInvariant(parameterName[0]) + parameterName[1..];

return new List<string>
{
Expand Down

0 comments on commit 7d6a411

Please sign in to comment.