2017-03-01 18:23:38 +08:00
// RUN: %clang_cc1 -fsyntax-only -Wunused-lambda-capture -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++1z %s
2017-01-13 23:01:06 +08:00
class NonTrivialConstructor {
public :
NonTrivialConstructor ( ) { }
} ;
2017-03-01 18:23:38 +08:00
class NonTrivialCopyConstructor {
public :
NonTrivialCopyConstructor ( ) = default ;
NonTrivialCopyConstructor ( const NonTrivialCopyConstructor & ) { }
} ;
2017-01-13 23:01:06 +08:00
class NonTrivialDestructor {
public :
~ NonTrivialDestructor ( ) { }
} ;
class Trivial {
public :
Trivial ( ) = default ;
Trivial ( int a ) { }
} ;
int side_effect ( ) {
return 42 ;
}
void test ( ) {
int i = 0 ;
2017-01-20 01:19:22 +08:00
const int k = 0 ;
2017-01-13 23:01:06 +08:00
auto captures_nothing = [ ] { } ;
auto captures_nothing_by_value = [ = ] { } ;
auto captures_nothing_by_reference = [ & ] { } ;
auto implicit_by_value = [ = ] ( ) mutable { i + + ; } ;
auto implicit_by_reference = [ & ] { i + + ; } ;
auto explicit_by_value_used = [ i ] { return i + 1 ; } ;
auto explicit_by_value_used_void = [ i ] { ( void ) i ; } ;
auto explicit_by_value_unused = [ i ] { } ; // expected-warning{{lambda capture 'i' is not used}}
2018-07-17 21:17:01 +08:00
auto explicit_by_value_unused_sizeof = [ i ] { return sizeof ( i ) ; } ; // expected-warning{{lambda capture 'i' is not required to be captured for this use}}
auto explicit_by_value_unused_decltype = [ i ] { decltype ( i ) j = 0 ; } ; // expected-warning{{lambda capture 'i' is not required to be captured for this use}}
auto explicit_by_value_unused_const = [ k ] { return k + 1 ; } ; // expected-warning{{lambda capture 'k' is not required to be captured for this use}}
2017-01-13 23:01:06 +08:00
auto explicit_by_reference_used = [ & i ] { i + + ; } ;
auto explicit_by_reference_unused = [ & i ] { } ; // expected-warning{{lambda capture 'i' is not used}}
auto explicit_initialized_reference_used = [ & j = i ] { return j + 1 ; } ;
auto explicit_initialized_reference_unused = [ & j = i ] { } ; // expected-warning{{lambda capture 'j' is not used}}
auto explicit_initialized_value_used = [ j = 1 ] { return j + 1 ; } ;
auto explicit_initialized_value_unused = [ j = 1 ] { } ; // expected-warning{{lambda capture 'j' is not used}}
auto explicit_initialized_value_non_trivial_constructor = [ j = NonTrivialConstructor ( ) ] { } ;
auto explicit_initialized_value_non_trivial_destructor = [ j = NonTrivialDestructor ( ) ] { } ;
auto explicit_initialized_value_trivial_init = [ j = Trivial ( ) ] { } ; // expected-warning{{lambda capture 'j' is not used}}
auto explicit_initialized_value_non_trivial_init = [ j = Trivial ( 42 ) ] { } ;
auto explicit_initialized_value_with_side_effect = [ j = side_effect ( ) ] { } ;
auto nested = [ & i ] {
auto explicit_by_value_used = [ i ] { return i + 1 ; } ;
auto explicit_by_value_unused = [ i ] { } ; // expected-warning{{lambda capture 'i' is not used}}
} ;
2017-03-01 18:23:38 +08:00
Trivial trivial ;
auto explicit_by_value_trivial = [ trivial ] { } ; // expected-warning{{lambda capture 'trivial' is not used}}
NonTrivialConstructor cons ;
auto explicit_by_value_non_trivial_constructor = [ cons ] { } ; // expected-warning{{lambda capture 'cons' is not used}}
NonTrivialCopyConstructor copy_cons ;
auto explicit_by_value_non_trivial_copy_constructor = [ copy_cons ] { } ;
NonTrivialDestructor dest ;
auto explicit_by_value_non_trivial_destructor = [ dest ] { } ;
volatile int v ;
auto explicit_by_value_volatile = [ v ] { } ;
2017-01-13 23:01:06 +08:00
}
2017-03-01 18:23:38 +08:00
class TrivialThis : Trivial {
2017-01-13 23:01:06 +08:00
void test ( ) {
auto explicit_this_used = [ this ] { return i ; } ;
auto explicit_this_used_void = [ this ] { ( void ) this ; } ;
auto explicit_this_unused = [ this ] { } ; // expected-warning{{lambda capture 'this' is not used}}
2017-03-01 18:23:38 +08:00
auto explicit_star_this_used = [ * this ] { return i ; } ;
auto explicit_star_this_used_void = [ * this ] { ( void ) this ; } ;
auto explicit_star_this_unused = [ * this ] { } ; // expected-warning{{lambda capture 'this' is not used}}
}
int i ;
} ;
class NonTrivialConstructorThis : NonTrivialConstructor {
void test ( ) {
auto explicit_this_used = [ this ] { return i ; } ;
auto explicit_this_used_void = [ this ] { ( void ) this ; } ;
auto explicit_this_unused = [ this ] { } ; // expected-warning{{lambda capture 'this' is not used}}
auto explicit_star_this_used = [ * this ] { return i ; } ;
auto explicit_star_this_used_void = [ * this ] { ( void ) this ; } ;
auto explicit_star_this_unused = [ * this ] { } ; // expected-warning{{lambda capture 'this' is not used}}
}
int i ;
} ;
class NonTrivialCopyConstructorThis : NonTrivialCopyConstructor {
void test ( ) {
auto explicit_this_used = [ this ] { return i ; } ;
auto explicit_this_used_void = [ this ] { ( void ) this ; } ;
auto explicit_this_unused = [ this ] { } ; // expected-warning{{lambda capture 'this' is not used}}
auto explicit_star_this_used = [ * this ] { return i ; } ;
auto explicit_star_this_used_void = [ * this ] { ( void ) this ; } ;
auto explicit_star_this_unused = [ * this ] { } ;
}
int i ;
} ;
class NonTrivialDestructorThis : NonTrivialDestructor {
void test ( ) {
auto explicit_this_used = [ this ] { return i ; } ;
auto explicit_this_used_void = [ this ] { ( void ) this ; } ;
auto explicit_this_unused = [ this ] { } ; // expected-warning{{lambda capture 'this' is not used}}
auto explicit_star_this_used = [ * this ] { return i ; } ;
auto explicit_star_this_used_void = [ * this ] { ( void ) this ; } ;
auto explicit_star_this_unused = [ * this ] { } ;
2017-01-13 23:01:06 +08:00
}
int i ;
} ;
template < typename T >
void test_templated ( ) {
int i = 0 ;
2017-01-20 01:19:22 +08:00
const int k = 0 ;
2017-01-13 23:01:06 +08:00
auto captures_nothing = [ ] { } ;
auto captures_nothing_by_value = [ = ] { } ;
auto captures_nothing_by_reference = [ & ] { } ;
auto implicit_by_value = [ = ] ( ) mutable { i + + ; } ;
auto implicit_by_reference = [ & ] { i + + ; } ;
auto explicit_by_value_used = [ i ] { return i + 1 ; } ;
2017-06-14 02:38:31 +08:00
auto explicit_by_value_used_generic = [ i ] ( auto c ) { return i + 1 ; } ;
2017-01-13 23:01:06 +08:00
auto explicit_by_value_used_void = [ i ] { ( void ) i ; } ;
2017-06-14 02:38:31 +08:00
2017-01-13 23:01:06 +08:00
auto explicit_by_value_unused = [ i ] { } ; // expected-warning{{lambda capture 'i' is not used}}
2018-07-17 21:17:01 +08:00
auto explicit_by_value_unused_sizeof = [ i ] { return sizeof ( i ) ; } ; // expected-warning{{lambda capture 'i' is not required to be captured for this use}}
2017-01-13 23:01:06 +08:00
auto explicit_by_value_unused_decltype = [ i ] { decltype ( i ) j = 0 ; } ; // expected-warning{{lambda capture 'i' is not used}}
2018-07-17 21:17:01 +08:00
auto explicit_by_value_unused_const = [ k ] { return k + 1 ; } ; // expected-warning{{lambda capture 'k' is not required to be captured for this use}}
auto explicit_by_value_unused_const_generic = [ k ] ( auto c ) { return k + 1 ; } ; // expected-warning{{lambda capture 'k' is not required to be captured for this use}}
2017-01-13 23:01:06 +08:00
auto explicit_by_reference_used = [ & i ] { i + + ; } ;
auto explicit_by_reference_unused = [ & i ] { } ; // expected-warning{{lambda capture 'i' is not used}}
auto explicit_initialized_reference_used = [ & j = i ] { return j + 1 ; } ;
auto explicit_initialized_reference_unused = [ & j = i ] { } ; // expected-warning{{lambda capture 'j' is not used}}
auto explicit_initialized_value_used = [ j = 1 ] { return j + 1 ; } ;
auto explicit_initialized_value_unused = [ j = 1 ] { } ; // expected-warning{{lambda capture 'j' is not used}}
auto explicit_initialized_value_non_trivial_constructor = [ j = NonTrivialConstructor ( ) ] { } ;
auto explicit_initialized_value_non_trivial_destructor = [ j = NonTrivialDestructor ( ) ] { } ;
auto explicit_initialized_value_trivial_init = [ j = Trivial ( ) ] { } ; // expected-warning{{lambda capture 'j' is not used}}
auto explicit_initialized_value_non_trivial_init = [ j = Trivial ( 42 ) ] { } ;
auto explicit_initialized_value_with_side_effect = [ j = side_effect ( ) ] { } ;
2017-06-14 02:38:31 +08:00
auto explicit_initialized_value_generic_used = [ i = 1 ] ( auto c ) mutable { i + + ; } ;
auto explicit_initialized_value_generic_unused = [ i = 1 ] ( auto c ) mutable { } ; // expected-warning{{lambda capture 'i' is not used}}
2017-01-13 23:01:06 +08:00
auto nested = [ & i ] {
auto explicit_by_value_used = [ i ] { return i + 1 ; } ;
auto explicit_by_value_unused = [ i ] { } ; // expected-warning{{lambda capture 'i' is not used}}
} ;
2017-03-01 18:23:38 +08:00
Trivial trivial ;
auto explicit_by_value_trivial = [ trivial ] { } ; // expected-warning{{lambda capture 'trivial' is not used}}
NonTrivialConstructor cons ;
auto explicit_by_value_non_trivial_constructor = [ cons ] { } ; // expected-warning{{lambda capture 'cons' is not used}}
NonTrivialCopyConstructor copy_cons ;
auto explicit_by_value_non_trivial_copy_constructor = [ copy_cons ] { } ;
NonTrivialDestructor dest ;
auto explicit_by_value_non_trivial_destructor = [ dest ] { } ;
volatile int v ;
auto explicit_by_value_volatile = [ v ] { } ;
2017-01-13 23:01:06 +08:00
}
void test_use_template ( ) {
test_templated < int > ( ) ; // expected-note{{in instantiation of function template specialization 'test_templated<int>' requested here}}
}
2017-12-12 02:00:36 +08:00
namespace pr35555 {
int a ;
void b ( ) {
int c [ a ] ;
auto vla_used = [ & c ] { return c [ 0 ] ; } ;
auto vla_unused = [ & c ] { } ; // expected-warning{{lambda capture 'c' is not used}}
}
} // namespace pr35555