cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[BUG] Autogenerated return after for triggers analysis error

Open MaxSagebaum opened this issue 3 months ago • 0 comments

If an automatic return statement is generated, e.g. for multiple return values, and the last statement is a for if loop. Then some analyzers will complain about the generated return. It is on the same line as the closing for if bracket.

The cpp2 code:

f: (x: std::vector<double>) -> (a:double, b: double) = {
  a = 0.0;
  b = 0.0;
  for x do (cur)
  if cur != 0.0 {
    a += cur;
    b += cur;
  }
}

Is translated to:



//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"

#line 1 "bug.cpp2"


//=== Cpp2 type definitions and function declarations ===========================

#line 1 "bug.cpp2"

struct f_ret { double a; double b; };

[[nodiscard]] auto f(cpp2::impl::in<std::vector<double>> x) -> f_ret;

//=== Cpp2 function definitions =================================================

#line 1 "bug.cpp2"
[[nodiscard]] auto f(cpp2::impl::in<std::vector<double>> x) -> f_ret{
      cpp2::impl::deferred_init<double> a;
      cpp2::impl::deferred_init<double> b;
#line 2 "bug.cpp2"
  a.construct(0.0);
  b.construct(0.0);
  for ( auto const& cur : x ) 
  if (cur != 0.0) {
    a.value() += cur;
    b.value() += cur;
  }return  { std::move(a.value()), std::move(b.value()) };  
}

With clang++ -Wall -pedantic -Werror -std=c++23 bug.cpp -Iinclude/ -c the error is generated:

bug.cpp2:8:4: warning: misleading indentation; statement is not part of the previous 'for' [-Wmisleading-indentation]
    8 |   }return  { std::move(a.value()), std::move(b.value()) }; 
      |    ^
bug.cpp2:4:3: note: previous statement is here
    4 |   for ( auto const& cur : x ) 
      |   ^
1 warning generated.

Press ENTER or type command to continue
bug.cpp2:8:4: error: misleading indentation; statement is not part of the previous 'for' [-Werror,-Wmisleading-indentation]
    8 |   }return  { std::move(a.value()), std::move(b.value()) }; 
      |    ^
bug.cpp2:4:3: note: previous statement is here
    4 |   for ( auto const& cur : x ) 
      |   ^
1 error generated.

Solution The simplest solution might be to add the omitted brackets to the for.

MaxSagebaum avatar Sep 25 '25 06:09 MaxSagebaum