Skip to content

Commit 15ecc13

Browse files
authored
Code QL Notes (#194)
1 parent b11e8d1 commit 15ecc13

File tree

8 files changed

+32
-48
lines changed

8 files changed

+32
-48
lines changed

src/AutoMapper.Extensions.ExpressionMapping/TypeMappingsManager.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,14 @@ public Type ReplaceType(Type sourceType)
8989
}
9090
else if (sourceType.IsGenericType)
9191
{
92-
if (TypeMappings.TryGetValue(sourceType, out Type destType))
93-
return destType;
94-
else
95-
{
96-
return sourceType.GetGenericTypeDefinition().MakeGenericType
92+
return TypeMappings.TryGetValue(sourceType, out Type destType)
93+
? destType
94+
: sourceType.GetGenericTypeDefinition().MakeGenericType
9795
(
9896
[.. sourceType
9997
.GetGenericArguments()
10098
.Select(ReplaceType)]
10199
);
102-
}
103100
}
104101
else
105102
{
@@ -182,11 +179,8 @@ private void FindChildPropertyTypeMaps(Type source, Type dest)
182179
FindMaps([.. typeMap.PropertyMaps]);
183180
void FindMaps(List<PropertyMap> maps)
184181
{
185-
foreach (PropertyMap pm in maps)
182+
foreach (PropertyMap pm in maps.Where(p => p.SourceMembers.Length > 0 || p.CustomMapExpression != null))
186183
{
187-
if (pm.SourceMembers.Length < 1 && pm.CustomMapExpression == null)
188-
continue;
189-
190184
AddChildMappings
191185
(
192186
source.GetFieldOrProperty(pm.DestinationMember.Name).GetMemberType(),

src/AutoMapper.Extensions.ExpressionMapping/XpressionMapperVisitor.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using AutoMapper.Extensions.ExpressionMapping.Structures;
44
using AutoMapper.Internal;
55
using System;
6-
using System.Collections;
76
using System.Collections.Generic;
87
using System.Globalization;
98
using System.Linq;
@@ -440,10 +439,9 @@ Expression DoVisitBinary(Expression newLeft, Expression newRight, Expression con
440439
{
441440
if (newLeft != node.Left || newRight != node.Right || conversion != node.Conversion)
442441
{
443-
if (node.NodeType == ExpressionType.Coalesce && node.Conversion != null)
444-
return Expression.Coalesce(newLeft, newRight, conversion as LambdaExpression);
445-
else
446-
return Expression.MakeBinary
442+
return node.NodeType == ExpressionType.Coalesce && node.Conversion != null
443+
? Expression.Coalesce(newLeft, newRight, conversion as LambdaExpression)
444+
: Expression.MakeBinary
447445
(
448446
node.NodeType,
449447
newLeft,

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/ExpressionMapping.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,21 @@ private void Should_Validate()
140140
[Fact]
141141
public void GrandParent_Mapping_To_Sub_Sub_Property_Condition()
142142
{
143-
Expression<Func<GrandParentDTO, bool>> _predicateExpression = gp => gp.Parent.Children.Any(c => c.ID2 == 3);
144-
var expression = Mapper.Map<Expression<Func<GrandParent, bool>>>(_predicateExpression);
143+
Expression<Func<GrandParentDTO, bool>> predicateExpression = gp => gp.Parent.Children.Any(c => c.ID2 == 3);
144+
var expression = Mapper.Map<Expression<Func<GrandParent, bool>>>(predicateExpression);
145145
var items = new[] {new GrandParent(){Parent = new Parent(){Children = [new Child(){ID2 = 3}], Child = new Child(){ID2 = 3}}}}.AsQueryable();
146146
items.Where(expression).ShouldContain(items.First());
147-
var items2 = items.UseAsDataSource(Mapper).For<GrandParentDTO>().Where(_predicateExpression);
147+
var items2 = items.UseAsDataSource(Mapper).For<GrandParentDTO>().Where(predicateExpression);
148148
items2.Count().ShouldBe(1);
149149
When_Use_Outside_Class_Method_Call();
150150
}
151151

152152
[Fact]
153153
public void GrandParent_Mapping_To_Sub_Sub_Property_Condition2()
154154
{
155-
Expression<Func<IQueryable<GrandParentDTO>, bool>> _predicateExpression = gps => gps.Any(gp => gp.Parent.Children.Any(c => c.ID_ == 3));
155+
Expression<Func<IQueryable<GrandParentDTO>, bool>> predicateExpression = gps => gps.Any(gp => gp.Parent.Children.Any(c => c.ID_ == 3));
156156
Expression<Func<IQueryable<GrandParentDTO>, IQueryable<GrandParentDTO>>> _predicateExpression2 = gps => gps.Where(gp => gp.Parent.Children.Any(c => c.ID_ == 3));
157-
var expression = Mapper.Map<Expression<Func<IQueryable<GrandParent>, bool>>>(_predicateExpression);
157+
var expression = Mapper.Map<Expression<Func<IQueryable<GrandParent>, bool>>>(predicateExpression);
158158
var expression1 = Mapper.Map<Expression<Func<IQueryable<GrandParent>, IQueryable<GrandParent>>>>(_predicateExpression2);
159159
expression.ShouldNotBeNull();
160160
expression1.ShouldNotBeNull();

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/Impl/SourceInjectedQuery.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public void Shoud_support_IEnumerable_result()
8282
.Where(s => s.DestValue > 6);
8383

8484
List<Destination> list = [.. result];
85+
Assert.NotEmpty(list);
8586
}
8687

8788
[Fact]

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/TypeExtensionsTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ public void GetDeclaredProperties_ReturnsAllDeclaredProperties()
207207

208208
private class ClassWithStaticMembers
209209
{
210-
public static int StaticField = 0;
211-
public int InstanceField = 0;
210+
public static readonly int StaticField = 0;
211+
public readonly int InstanceField = 0;
212212
public static int StaticProperty { get; set; }
213213
public int InstanceProperty { get; set; }
214214
public static void StaticMethod() { }

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/TypeMappingsManagerTest.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Linq.Expressions;
5-
using AutoMapper;
64
using Xunit;
75

86
namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
@@ -118,8 +116,7 @@ public void Constructor_AddsMappingsFromDelegateTypes()
118116
typeof(Func<DestModel, bool>));
119117

120118
// Assert
121-
Assert.True(manager.TypeMappings.ContainsKey(typeof(SourceModel)));
122-
Assert.Equal(typeof(DestModel), manager.TypeMappings[typeof(SourceModel)]);
119+
Assert.Contains(new KeyValuePair<Type, Type>(typeof(SourceModel), typeof(DestModel)), manager.TypeMappings);
123120
}
124121

125122
#endregion
@@ -143,8 +140,7 @@ public void AddTypeMapping_SimpleTypes_AddsMapping()
143140
manager.AddTypeMapping(typeof(SourceChild), typeof(DestChild));
144141

145142
// Assert
146-
Assert.True(manager.TypeMappings.ContainsKey(typeof(SourceChild)));
147-
Assert.Equal(typeof(DestChild), manager.TypeMappings[typeof(SourceChild)]);
143+
Assert.Contains(new KeyValuePair<Type, Type>(typeof(SourceChild), typeof(DestChild)), manager.TypeMappings);
148144
}
149145

150146
[Fact]
@@ -187,8 +183,7 @@ public void AddTypeMapping_ExpressionTypes_UnwrapsAndAddsMapping()
187183
typeof(Expression<Func<DestChild, bool>>));
188184

189185
// Assert
190-
Assert.True(manager.TypeMappings.ContainsKey(typeof(Func<SourceChild, bool>)));
191-
Assert.Equal(typeof(Func<DestChild, bool>), manager.TypeMappings[typeof(Func<SourceChild, bool>)]);
186+
Assert.Contains(new KeyValuePair<Type, Type>(typeof(Func<SourceChild, bool>), typeof(Func<DestChild, bool>)), manager.TypeMappings);
192187
}
193188

194189
[Fact]
@@ -231,9 +226,8 @@ public void AddTypeMapping_ListTypes_AddsUnderlyingTypeMappings()
231226
manager.AddTypeMapping(typeof(List<SourceItem>), typeof(List<DestItem>));
232227

233228
// Assert
234-
Assert.True(manager.TypeMappings.ContainsKey(typeof(List<SourceItem>)));
235-
Assert.True(manager.TypeMappings.ContainsKey(typeof(SourceItem)));
236-
Assert.Equal(typeof(DestItem), manager.TypeMappings[typeof(SourceItem)]);
229+
Assert.Contains(new KeyValuePair<Type, Type>(typeof(SourceItem), typeof(DestItem)), manager.TypeMappings);
230+
Assert.Contains(new KeyValuePair<Type, Type>(typeof(List<SourceItem>), typeof(List<DestItem>)), manager.TypeMappings);
237231
}
238232

239233
[Fact]
@@ -254,9 +248,8 @@ public void AddTypeMapping_ArrayTypes_AddsUnderlyingTypeMappings()
254248
manager.AddTypeMapping(typeof(SourceItem[]), typeof(DestItem[]));
255249

256250
// Assert
257-
Assert.True(manager.TypeMappings.ContainsKey(typeof(SourceItem[])));
258-
Assert.True(manager.TypeMappings.ContainsKey(typeof(SourceItem)));
259-
Assert.Equal(typeof(DestItem), manager.TypeMappings[typeof(SourceItem)]);
251+
Assert.Contains(new KeyValuePair<Type, Type>(typeof(SourceItem), typeof(DestItem)), manager.TypeMappings);
252+
Assert.Contains(new KeyValuePair<Type, Type>(typeof(SourceItem[]), typeof(DestItem[])), manager.TypeMappings);
260253
}
261254

262255
#endregion
@@ -589,8 +582,7 @@ public void AddTypeMappingsFromDelegates_MatchingArgumentCounts_AddsMappings()
589582
typeof(Func<DestChild, string>));
590583

591584
// Assert
592-
Assert.True(manager.TypeMappings.ContainsKey(typeof(SourceChild)));
593-
Assert.Equal(typeof(DestChild), manager.TypeMappings[typeof(SourceChild)]);
585+
Assert.Contains(new KeyValuePair<Type, Type>(typeof(SourceChild), typeof(DestChild)), manager.TypeMappings);
594586
}
595587

596588
[Fact]
@@ -633,8 +625,7 @@ public void AddTypeMappingsFromDelegates_ActionTypes_AddsMappings()
633625
typeof(Action<DestChild>));
634626

635627
// Assert
636-
Assert.True(manager.TypeMappings.ContainsKey(typeof(SourceChild)));
637-
Assert.Equal(typeof(DestChild), manager.TypeMappings[typeof(SourceChild)]);
628+
Assert.Contains(new KeyValuePair<Type, Type>(typeof(SourceChild), typeof(DestChild)), manager.TypeMappings);
638629
}
639630

640631
#endregion

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/XpressionMapper.Structs.Tests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public static implicit operator Source(int i)
301301

302302
public override readonly int GetHashCode()
303303
{
304-
return this.val.GetHashCode();
304+
return this.val;
305305
}
306306

307307
public override readonly bool Equals(object obj)
@@ -370,7 +370,7 @@ public static implicit operator Dest(int i)
370370

371371
public override readonly int GetHashCode()
372372
{
373-
return this.val.GetHashCode();
373+
return this.val;
374374
}
375375

376376
public override readonly bool Equals(object obj)
@@ -417,7 +417,7 @@ public override readonly bool Equals(object obj)
417417
}
418418
public override readonly int GetHashCode()
419419
{
420-
return Year.GetHashCode();
420+
return Year;
421421
}
422422
}
423423

@@ -444,7 +444,7 @@ public override readonly bool Equals(object obj)
444444
}
445445
public override readonly int GetHashCode()
446446
{
447-
return Year.GetHashCode();
447+
return Year;
448448
}
449449
}
450450

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/XpressionMapperTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,10 @@ public void Map_where_multiple_arguments()
416416

417417
//Act
418418
Expression<Func<User, Account, object>> selectionMapped = mapper.Map<Expression<Func<User, Account, object>>>(selection);
419-
object val = selectionMapped.Compile().Invoke(Users.ToList()[0], Users.ToList()[0].Account);
419+
object valLocal = selectionMapped.Compile().Invoke(Users.ToList()[0], Users.ToList()[0].Account);
420420

421421
//Assert
422-
Assert.False((bool)val);
422+
Assert.False((bool)valLocal);
423423
}
424424

425425
[Fact]
@@ -838,9 +838,9 @@ public void Can_map_expressions_with_action_independent_of_expression_param()
838838
}
839839

840840
object val;
841-
void CallSomeAction<T>(T val)
841+
void CallSomeAction<T>(T valParam)
842842
{
843-
this.val = val;
843+
this.val = valParam;
844844
}
845845

846846
[Fact]

0 commit comments

Comments
 (0)