Skip to content

fix(to_cpp1): don't emit [[nodiscard]] for pre-increment/decrement #883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -2229,6 +2229,9 @@ struct function_type_node
auto is_comparison() const
-> bool;

auto is_increment_or_decrement() const
-> bool;

auto is_compound_assignment() const
-> bool;

Expand Down Expand Up @@ -3197,6 +3200,16 @@ struct declaration_node
return false;
}

auto is_increment_or_decrement() const
-> bool
{
if (auto func = std::get_if<a_function>(&type)) {
return (*func)->is_increment_or_decrement();
}
// else
return false;
}

auto is_compound_assignment() const
-> bool
{
Expand Down Expand Up @@ -3709,6 +3722,20 @@ auto function_type_node::is_comparison() const
}


auto function_type_node::is_increment_or_decrement() const
-> bool
{
if (
my_decl->has_name("operator++")
|| my_decl->has_name("operator--")
)
{
return true;
}
return false;
}


auto function_type_node::is_compound_assignment() const
-> bool
{
Expand Down
28 changes: 28 additions & 0 deletions source/sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,34 @@ class sema
}


auto check(function_type_node const& n)
-> bool
{
// An increment/decrement function must have a single parameter that is 'inout this'
if (
(
n.my_decl->has_name("operator++")
|| n.my_decl->has_name("operator--")
)
&&
(
(*n.parameters).ssize() != 1
|| !(*n.parameters)[0]->has_name("this")
|| (*n.parameters)[0]->direction() != passing_style::inout
)
)
{
errors.emplace_back(
n.position(),
"a user-defined " + n.my_decl->name()->to_string() + " must have a single 'inout this' parameter"
);
return false;
}

return true;
}


auto check(statement_node const& n)
-> bool
{
Expand Down
5 changes: 5 additions & 0 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -4596,6 +4596,10 @@ class cppfront
{
assert(n.parameters);

if (!sema.check(n)) {
return;
}

if (
is_main
&& n.parameters->parameters.size() > 0
Expand Down Expand Up @@ -6093,6 +6097,7 @@ class cppfront
func->has_non_void_return_type()
&& !func->is_assignment()
&& !func->is_compound_assignment()
&& !func->is_increment_or_decrement()
&& (
printer.get_phase() == printer.phase1_type_defs_func_decls
|| n.has_initializer() // so we're printing it in phase 2
Expand Down