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

https://gitlab.com/pranith/rust · Rust · 120 lines · 90 code · 17 blank · 13 comment · 1 complexity · e297b5aa0b6ac7c717c4d3d118330888 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. #![feature(no_std)]
  11. #![no_std]
  12. #![allow(unused_variables)]
  13. #![allow(non_camel_case_types)]
  14. #![allow(non_upper_case_globals)]
  15. #![deny(dead_code)]
  16. #![feature(core)]
  17. #![crate_type="lib"]
  18. extern crate core;
  19. pub use foo2::Bar2;
  20. mod foo {
  21. pub struct Bar; //~ ERROR: struct is never used
  22. }
  23. mod foo2 {
  24. pub struct Bar2;
  25. }
  26. pub static pub_static: isize = 0;
  27. static priv_static: isize = 0; //~ ERROR: static item is never used
  28. const used_static: isize = 0;
  29. pub static used_static2: isize = used_static;
  30. const USED_STATIC: isize = 0;
  31. const STATIC_USED_IN_ENUM_DISCRIMINANT: isize = 10;
  32. pub const pub_const: isize = 0;
  33. const priv_const: isize = 0; //~ ERROR: constant item is never used
  34. const used_const: isize = 0;
  35. pub const used_const2: isize = used_const;
  36. const USED_CONST: isize = 1;
  37. const CONST_USED_IN_ENUM_DISCRIMINANT: isize = 11;
  38. pub type typ = *const UsedStruct4;
  39. pub struct PubStruct;
  40. struct PrivStruct; //~ ERROR: struct is never used
  41. struct UsedStruct1 {
  42. #[allow(dead_code)]
  43. x: isize
  44. }
  45. struct UsedStruct2(isize);
  46. struct UsedStruct3;
  47. struct UsedStruct4;
  48. // this struct is never used directly, but its method is, so we don't want
  49. // to warn it
  50. struct SemiUsedStruct;
  51. impl SemiUsedStruct {
  52. fn la_la_la() {}
  53. }
  54. struct StructUsedAsField;
  55. pub struct StructUsedInEnum;
  56. struct StructUsedInGeneric;
  57. pub struct PubStruct2 {
  58. #[allow(dead_code)]
  59. struct_used_as_field: *const StructUsedAsField
  60. }
  61. pub enum pub_enum { foo1, bar1 }
  62. pub enum pub_enum2 { a(*const StructUsedInEnum) }
  63. pub enum pub_enum3 {
  64. Foo = STATIC_USED_IN_ENUM_DISCRIMINANT,
  65. Bar = CONST_USED_IN_ENUM_DISCRIMINANT,
  66. }
  67. enum priv_enum { foo2, bar2 } //~ ERROR: enum is never used
  68. enum used_enum {
  69. foo3,
  70. bar3 //~ ERROR variant is never used
  71. }
  72. fn f<T>() {}
  73. pub fn pub_fn() {
  74. used_fn();
  75. let used_struct1 = UsedStruct1 { x: 1 };
  76. let used_struct2 = UsedStruct2(1);
  77. let used_struct3 = UsedStruct3;
  78. let e = used_enum::foo3;
  79. SemiUsedStruct::la_la_la();
  80. let i = 1;
  81. match i {
  82. USED_STATIC => (),
  83. USED_CONST => (),
  84. _ => ()
  85. }
  86. f::<StructUsedInGeneric>();
  87. }
  88. fn priv_fn() { //~ ERROR: function is never used
  89. let unused_struct = PrivStruct;
  90. }
  91. fn used_fn() {}
  92. fn foo() { //~ ERROR: function is never used
  93. bar();
  94. let unused_enum = priv_enum::foo2;
  95. }
  96. fn bar() { //~ ERROR: function is never used
  97. foo();
  98. }
  99. // Code with #[allow(dead_code)] should be marked live (and thus anything it
  100. // calls is marked live)
  101. #[allow(dead_code)]
  102. fn g() { h(); }
  103. fn h() {}