@@ -10714,6 +10714,20 @@ Expression *AssignExp::semantic(Scope *sc)
10714
10714
e2 = new SliceExp (e2 -> loc , e2 , NULL , NULL );
10715
10715
e2 = e2 -> semantic (sc );
10716
10716
}
10717
+ else if (global .params .warnings && !global .gag && op == TOKassign &&
10718
+ e2 -> op != TOKarrayliteral && e2 -> op != TOKstring )
10719
+ { // Disallow sa = da (Converted to sa[] = da[])
10720
+ // Disallow sa = e (Converted to sa[] = e)
10721
+ const char * e1str = e1 -> toChars ();
10722
+ const char * e2str = e2 -> toChars ();
10723
+ if (e2 -> op == TOKslice || t2 -> implicitConvTo (t1 -> nextOf ()))
10724
+ warning ("explicit element-wise assignment (%s)[] = %s is better than %s = %s" ,
10725
+ e1str , e2str , e1str , e2str );
10726
+ else
10727
+ warning ("explicit element-wise assignment (%s)[] = (%s)[] is better than %s = %s" ,
10728
+ e1str , e2str , e1str , e2str );
10729
+ return new ErrorExp ();
10730
+ }
10717
10731
10718
10732
// Convert e1 to e1[]
10719
10733
Expression * e = new SliceExp (e1 -> loc , e1 , NULL , NULL );
@@ -10809,13 +10823,44 @@ Expression *AssignExp::semantic(Scope *sc)
10809
10823
{
10810
10824
checkPostblit (e2 -> loc , t2 -> nextOf ());
10811
10825
}
10826
+ if (global .params .warnings && !global .gag && op == TOKassign &&
10827
+ e2 -> op != TOKslice && e2 -> op != TOKassign &&
10828
+ e2 -> op != TOKarrayliteral && e2 -> op != TOKstring &&
10829
+ !(e2 -> op == TOKadd || e2 -> op == TOKmin ||
10830
+ e2 -> op == TOKmul || e2 -> op == TOKdiv ||
10831
+ e2 -> op == TOKmod || e2 -> op == TOKxor ||
10832
+ e2 -> op == TOKand || e2 -> op == TOKor ||
10833
+ #if DMDV2
10834
+ e2 -> op == TOKpow ||
10835
+ #endif
10836
+ e2 -> op == TOKtilde || e2 -> op == TOKneg ))
10837
+ {
10838
+ const char * e1str = e1 -> toChars ();
10839
+ const char * e2str = e2 -> toChars ();
10840
+ warning ("explicit element-wise assignment %s = (%s)[] is better than %s = %s" ,
10841
+ e1str , e2str , e1str , e2str );
10842
+ return new ErrorExp ();
10843
+ }
10812
10844
if (op == TOKconstruct )
10813
10845
e2 = e2 -> castTo (sc , e1 -> type -> constOf ());
10814
10846
else
10815
10847
e2 = e2 -> implicitCastTo (sc , e1 -> type -> constOf ());
10816
10848
}
10817
10849
else
10818
10850
{
10851
+ if (global .params .warnings && !global .gag && op == TOKassign &&
10852
+ t1 -> ty == Tarray && t2 -> ty == Tsarray &&
10853
+ e2 -> op != TOKslice && //e2->op != TOKarrayliteral &&
10854
+ t2 -> implicitConvTo (t1 ))
10855
+ { // Disallow ar[] = sa (Converted to ar[] = sa[])
10856
+ // Disallow da = sa (Converted to da = sa[])
10857
+ const char * e1str = e1 -> toChars ();
10858
+ const char * e2str = e2 -> toChars ();
10859
+ warning ("explicit %s assignment %s = (%s)[] is better than %s = %s" ,
10860
+ e1 -> op == TOKslice ? "element-wise" : "slice" ,
10861
+ e1str , e2str , e1str , e2str );
10862
+ return new ErrorExp ();
10863
+ }
10819
10864
e2 = e2 -> implicitCastTo (sc , e1 -> type );
10820
10865
}
10821
10866
if (e2 -> op == TOKerror )
0 commit comments