/src/test/compile-fail/lint-dead-code-1.rs

https://gitlab.com/alx741/rust · Rust · 116 lines · 87 code · 16 blank · 13 comment · 1 complexity · 7c501453dd5535218c5a3a7180224e0a MD5 · raw file

  1. // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. #![no_std]
  11. #![allow(unused_variables)]
  12. #![allow(non_camel_case_types)]
  13. #![allow(non_upper_case_globals)]
  14. #![deny(dead_code)]
  15. #![crate_type="lib"]
  16. pub use foo2::Bar2;
  17. mod foo {
  18. pub struct Bar; //~ ERROR: struct is never used
  19. }
  20. mod foo2 {
  21. pub struct Bar2;
  22. }
  23. pub static pub_static: isize = 0;
  24. static priv_static: isize = 0; //~ ERROR: static item is never used
  25. const used_static: isize = 0;
  26. pub static used_static2: isize = used_static;
  27. const USED_STATIC: isize = 0;
  28. const STATIC_USED_IN_ENUM_DISCRIMINANT: isize = 10;
  29. pub const pub_const: isize = 0;
  30. const priv_const: isize = 0; //~ ERROR: constant item is never used
  31. const used_const: isize = 0;
  32. pub const used_const2: isize = used_const;
  33. const USED_CONST: isize = 1;
  34. const CONST_USED_IN_ENUM_DISCRIMINANT: isize = 11;
  35. pub type typ = *const UsedStruct4;
  36. pub struct PubStruct;
  37. struct PrivStruct; //~ ERROR: struct is never used
  38. struct UsedStruct1 {
  39. #[allow(dead_code)]
  40. x: isize
  41. }
  42. struct UsedStruct2(isize);
  43. struct UsedStruct3;
  44. pub struct UsedStruct4;
  45. // this struct is never used directly, but its method is, so we don't want
  46. // to warn it
  47. struct SemiUsedStruct;
  48. impl SemiUsedStruct {
  49. fn la_la_la() {}
  50. }
  51. struct StructUsedAsField;
  52. pub struct StructUsedInEnum;
  53. struct StructUsedInGeneric;
  54. pub struct PubStruct2 {
  55. #[allow(dead_code)]
  56. struct_used_as_field: *const StructUsedAsField
  57. }
  58. pub enum pub_enum { foo1, bar1 }
  59. pub enum pub_enum2 { a(*const StructUsedInEnum) }
  60. pub enum pub_enum3 {
  61. Foo = STATIC_USED_IN_ENUM_DISCRIMINANT,
  62. Bar = CONST_USED_IN_ENUM_DISCRIMINANT,
  63. }
  64. enum priv_enum { foo2, bar2 } //~ ERROR: enum is never used
  65. enum used_enum {
  66. foo3,
  67. bar3 //~ ERROR variant is never used
  68. }
  69. fn f<T>() {}
  70. pub fn pub_fn() {
  71. used_fn();
  72. let used_struct1 = UsedStruct1 { x: 1 };
  73. let used_struct2 = UsedStruct2(1);
  74. let used_struct3 = UsedStruct3;
  75. let e = used_enum::foo3;
  76. SemiUsedStruct::la_la_la();
  77. let i = 1;
  78. match i {
  79. USED_STATIC => (),
  80. USED_CONST => (),
  81. _ => ()
  82. }
  83. f::<StructUsedInGeneric>();
  84. }
  85. fn priv_fn() { //~ ERROR: function is never used
  86. let unused_struct = PrivStruct;
  87. }
  88. fn used_fn() {}
  89. fn foo() { //~ ERROR: function is never used
  90. bar();
  91. let unused_enum = priv_enum::foo2;
  92. }
  93. fn bar() { //~ ERROR: function is never used
  94. foo();
  95. }
  96. // Code with #[allow(dead_code)] should be marked live (and thus anything it
  97. // calls is marked live)
  98. #[allow(dead_code)]
  99. fn g() { h(); }
  100. fn h() {}