|
| 1 | +// Take a look at the license at the top of the repository in the LICENSE file. |
| 2 | + |
| 3 | +// We want to ensure that all subclasses have: |
| 4 | +// |
| 5 | +// ``` |
| 6 | +// if !crate::rt::is_initialized() { |
| 7 | +// panic!("GTK has to be initialized first"); |
| 8 | +// } |
| 9 | +// ``` |
| 10 | + |
| 11 | +use std::fs::{read_dir, read_to_string}; |
| 12 | +use std::path::Path; |
| 13 | + |
| 14 | +fn check_file(f: &Path) -> usize { |
| 15 | + let s = read_to_string(f).expect("cannot read file"); |
| 16 | + let mut checking_type: Option<String> = None; |
| 17 | + let mut found_is_initialized_check = false; |
| 18 | + |
| 19 | + for line in s.lines() { |
| 20 | + if checking_type.is_none() { |
| 21 | + if !line.starts_with("unsafe impl") || !line.contains(" IsSubclassable<") { |
| 22 | + continue; |
| 23 | + } |
| 24 | + if let Some(for_) = line |
| 25 | + .split(" for ") |
| 26 | + .nth(1) |
| 27 | + .and_then(|s| s.split(' ').next()) |
| 28 | + { |
| 29 | + checking_type = Some(for_.to_owned()); |
| 30 | + } |
| 31 | + continue; |
| 32 | + } |
| 33 | + if line == "}" { |
| 34 | + // Analysis complete! |
| 35 | + break; |
| 36 | + } |
| 37 | + let trimmed = line.trim(); |
| 38 | + if trimmed.contains("is_initialized()") { |
| 39 | + found_is_initialized_check = true; |
| 40 | + } |
| 41 | + } |
| 42 | + if !found_is_initialized_check { |
| 43 | + if let Some(for_) = checking_type { |
| 44 | + if for_ == "Application" { |
| 45 | + return 0; |
| 46 | + } |
| 47 | + eprintln!( |
| 48 | + "[{}] Missing `is_initialized()` check in subclass implementation (`{}`)", |
| 49 | + f.display(), |
| 50 | + for_ |
| 51 | + ); |
| 52 | + } else { |
| 53 | + eprintln!( |
| 54 | + "Missing `IsSubclassable` implementation in `{}`", |
| 55 | + f.display() |
| 56 | + ) |
| 57 | + } |
| 58 | + 1 |
| 59 | + } else { |
| 60 | + 0 |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[test] |
| 65 | +fn check_subclass_panic() { |
| 66 | + let mut errors = 0; |
| 67 | + |
| 68 | + for entry in read_dir("src/subclass").unwrap() { |
| 69 | + let entry = entry.expect("invalid entry"); |
| 70 | + let path = entry.path(); |
| 71 | + if path.is_file() { |
| 72 | + errors += check_file(&path); |
| 73 | + } |
| 74 | + } |
| 75 | + assert!(errors == 0); |
| 76 | +} |
0 commit comments