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

https://gitlab.com/jianglu/rust · Rust · 88 lines · 56 code · 19 blank · 13 comment · 1 complexity · 067d69cb41e0366c296274adf5754b4b 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. #![allow(unused_variables)]
  11. #![allow(non_camel_case_types)]
  12. #![deny(dead_code)]
  13. #![crate_type="lib"]
  14. pub use extern_foo as x;
  15. extern {
  16. pub fn extern_foo();
  17. }
  18. struct Foo; //~ ERROR: struct is never used
  19. impl Foo {
  20. fn foo(&self) { //~ ERROR: method is never used
  21. bar()
  22. }
  23. }
  24. fn bar() { //~ ERROR: function is never used
  25. fn baz() {}
  26. Foo.foo();
  27. baz();
  28. }
  29. // no warning
  30. struct Foo2;
  31. impl Foo2 { fn foo2(&self) { bar2() } }
  32. fn bar2() {
  33. fn baz2() {}
  34. Foo2.foo2();
  35. baz2();
  36. }
  37. pub fn pub_fn() {
  38. let foo2_struct = Foo2;
  39. foo2_struct.foo2();
  40. blah::baz();
  41. }
  42. mod blah {
  43. // not warned because it's used in the parameter of `free` and return of
  44. // `malloc` below, which are also used.
  45. enum c_void {}
  46. extern {
  47. fn free(p: *const c_void);
  48. fn malloc(size: usize) -> *const c_void;
  49. }
  50. pub fn baz() {
  51. unsafe { free(malloc(4)); }
  52. }
  53. }
  54. enum c_void {} //~ ERROR: enum is never used
  55. extern {
  56. fn free(p: *const c_void); //~ ERROR: foreign function is never used
  57. }
  58. // Check provided method
  59. mod inner {
  60. pub trait Trait {
  61. fn f(&self) { f(); }
  62. }
  63. impl Trait for isize {}
  64. fn f() {}
  65. }
  66. pub fn foo() {
  67. let a: &inner::Trait = &1_isize;
  68. a.f();
  69. }