THRIFT-5198 Fix certain Visual Studio hints in generated netstd code
Client: netstd
Patch: Jens Geyer

This closes #2133
diff --git a/compiler/cpp/src/thrift/generate/t_netstd_generator.cc b/compiler/cpp/src/thrift/generate/t_netstd_generator.cc
index 6737862..8b6389f 100644
--- a/compiler/cpp/src/thrift/generate/t_netstd_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_netstd_generator.cc
@@ -316,8 +316,16 @@
     netstd_keywords["when"] = 1;
 }
 
+void t_netstd_generator::reset_indent() {
+  while( indent_count() > 0) { 
+    indent_down(); 
+  }
+}
+
+
 void t_netstd_generator::start_netstd_namespace(ostream& out)
 {
+    out << "#pragma warning disable IDE1006  // parts of the code use IDL spelling" << endl;
     if (!namespace_name_.empty())
     {
         out << "namespace " << namespace_name_ << endl;
@@ -331,6 +339,7 @@
     {
         scope_down(out);
     }
+    out << "#pragma warning restore IDE1006" << endl;
 }
 
 string t_netstd_generator::netstd_type_usings() const
@@ -394,6 +403,7 @@
 
 void t_netstd_generator::generate_enum(ostream& out, t_enum* tenum)
 {
+    reset_indent();
     out << autogen_comment() << endl;
 
     start_netstd_namespace(out);
@@ -439,6 +449,7 @@
         return;
     }
 
+    reset_indent();
     out << autogen_comment() << netstd_type_usings() << endl;
 
     start_netstd_namespace(out);
@@ -676,6 +687,7 @@
 
     f_struct.open(f_struct_name.c_str());
 
+  reset_indent();
     f_struct << autogen_comment() << netstd_type_usings() << netstd_thrift_usings() << endl;
 
     generate_netstd_struct_definition(f_struct, tstruct, is_exception);
@@ -1200,11 +1212,12 @@
     vector<t_field*>::const_iterator f_iter;
 
     bool useFirstFlag = false;
+    string tmp_count = tmp("tmp");
     for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter)
     {
         if (!field_is_required((*f_iter)))
         {
-            out << indent() << "bool __first = true;" << endl;
+            out << indent() << "int " << tmp_count.c_str() << " = 0;" << endl;
             useFirstFlag = true;
         }
         break;
@@ -1234,11 +1247,7 @@
 
         if (useFirstFlag && (!had_required))
         {
-            out << indent() << "if(!__first) { sb.Append(\", \"); }" << endl;
-            if (!is_required)
-            {
-                out << indent() << "__first = false;" << endl;
-            }
+            out << indent() << "if(0 < " << tmp_count.c_str() << (is_required ? "" : "++") << ") { sb.Append(\", \"); }" << endl;
             out << indent() << "sb.Append(\"" << prop_name(*f_iter) << ": \");" << endl;
         }
         else
@@ -1255,7 +1264,7 @@
         }
         else
         {
-            had_required = true; // now __first must be false, so we don't need to check it anymore
+            had_required = true; // now __count must be > 0, so we don't need to check it anymore
         }
     }
 
@@ -1274,6 +1283,7 @@
 
     f_union.open(f_union_name.c_str());
 
+    reset_indent();
     f_union << autogen_comment() << netstd_type_usings() << netstd_thrift_usings() << endl;
 
     generate_netstd_union_definition(f_union, tunion);
@@ -1403,10 +1413,10 @@
     out << indent() << "public override bool Equals(object that)" << endl
         << indent() << "{" << endl;
     indent_up();
-    out << indent() << "var other = that as " << check_and_correct_struct_name(normalize_name(tstruct->get_name())) << ";" << endl
-        << indent() << "if (other == null) return false;" << endl
+    out << indent() << "if (!(that is " << check_and_correct_struct_name(normalize_name(tstruct->get_name())) << " other)) return false;" << endl
         << indent() << "if (ReferenceEquals(this, other)) return true;" << endl;
 
+
     const vector<t_field*>& fields = tstruct->get_members();
     vector<t_field*>::const_iterator f_iter;
 
@@ -1488,7 +1498,7 @@
         else
         {
             out << prop_name((*f_iter)) << ".GetHashCode()";
-        }	
+        }
         out << ";" << endl;
 
         if (!field_is_required((*f_iter)))
@@ -1513,6 +1523,7 @@
     ofstream_with_content_based_conditional_update f_service;
     f_service.open(f_service_name.c_str());
 
+    reset_indent();
     f_service << autogen_comment() << netstd_type_usings() << netstd_thrift_usings() << endl;
 
     start_netstd_namespace(f_service);
@@ -1641,9 +1652,11 @@
         string argsname = (*functions_iterator)->get_name() + "Args";
 
         out << indent() << "await OutputProtocol.WriteMessageBeginAsync(new TMessage(\"" << function_name
-            << "\", " << ((*functions_iterator)->is_oneway() ? "TMessageType.Oneway" : "TMessageType.Call") << ", SeqId), cancellationToken);" << endl
+            << "\", TMessageType." << ((*functions_iterator)->is_oneway() ? "Oneway" : "Call") 
+            << ", SeqId), cancellationToken);" << endl
             << indent() << endl
-            << indent() << "var args = new " << argsname << "();" << endl;
+            << indent() << "var args = new " << argsname << "() {" << endl;
+        indent_up();
 
         t_struct* arg_struct = (*functions_iterator)->get_arglist();
         prepare_member_name_mapping(arg_struct);
@@ -1652,9 +1665,13 @@
 
         for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter)
         {
-            out << indent() << "args." << prop_name(*fld_iter) << " = " << normalize_name((*fld_iter)->get_name()) << ";" << endl;
+            out << indent() << prop_name(*fld_iter) << " = " << normalize_name((*fld_iter)->get_name()) << "," << endl;
         }
 
+        indent_down();
+        out << indent() << "};" << endl;
+
+
         out << indent() << endl
             << indent() << "await args.WriteAsync(OutputProtocol, cancellationToken);" << endl
             << indent() << "await OutputProtocol.WriteMessageEndAsync(cancellationToken);" << endl
@@ -1749,7 +1766,7 @@
 
     indent_up();
 
-    out << indent() << "private IAsync _iAsync;" << endl
+    out << indent() << "private readonly IAsync _iAsync;" << endl
         << endl
         << indent() << "public AsyncProcessor(IAsync iAsync)";
 
@@ -1762,10 +1779,7 @@
         << indent() << "{" << endl;
     indent_up();
 
-    out << indent() << "if (iAsync == null) throw new ArgumentNullException(nameof(iAsync));" << endl
-        << endl
-        << indent() << "_iAsync = iAsync;" << endl;
-
+    out << indent() << "_iAsync = iAsync ?? throw new ArgumentNullException(nameof(iAsync));" << endl;
     for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter)
     {
         string function_name = (*f_iter)->get_name();
@@ -1818,8 +1832,7 @@
     indent_up();
     out << indent() << "var msg = await iprot.ReadMessageBeginAsync(cancellationToken);" << endl
         << endl
-        << indent() << "ProcessFunction fn;" << endl
-        << indent() << "processMap_.TryGetValue(msg.Name, out fn);" << endl
+        << indent() << "processMap_.TryGetValue(msg.Name, out ProcessFunction fn);" << endl
         << endl
         << indent() << "if (fn == null)" << endl
         << indent() << "{" << endl;
@@ -2856,7 +2869,7 @@
     {
         result += ", ";
     }
-    result += "CancellationToken cancellationToken = default(CancellationToken))";
+    result += "CancellationToken cancellationToken = default)";
 
     return result;
 }
diff --git a/compiler/cpp/src/thrift/generate/t_netstd_generator.h b/compiler/cpp/src/thrift/generate/t_netstd_generator.h
index 1e23f91..30082e8 100644
--- a/compiler/cpp/src/thrift/generate/t_netstd_generator.h
+++ b/compiler/cpp/src/thrift/generate/t_netstd_generator.h
@@ -159,4 +159,5 @@
   void prepare_member_name_mapping(void* scope, const vector<t_field*>& members, const string& structname);
   void cleanup_member_name_mapping(void* scope);
   string get_mapped_member_name(string oldname);
+  void reset_indent();
 };