unused_variable_detection.rs 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. // SPDX-License-Identifier: Apache-2.0
  2. use solang::file_resolver::FileResolver;
  3. use solang::sema::ast;
  4. use solang::{parse_and_resolve, Target};
  5. use std::ffi::OsStr;
  6. fn parse(src: &'static str) -> ast::Namespace {
  7. let mut cache = FileResolver::default();
  8. cache.set_file_contents("test.sol", src.to_string());
  9. parse_and_resolve(OsStr::new("test.sol"), &mut cache, Target::EVM)
  10. }
  11. fn parse_two_files(src1: &'static str, src2: &'static str) -> ast::Namespace {
  12. let mut cache = FileResolver::default();
  13. cache.set_file_contents("test.sol", src1.to_string());
  14. cache.set_file_contents("test2.sol", src2.to_string());
  15. parse_and_resolve(OsStr::new("test.sol"), &mut cache, Target::EVM)
  16. }
  17. #[test]
  18. fn emit_event() {
  19. //Used event
  20. let case_1 = r#"
  21. contract usedEvent {
  22. event Hey(uint8 n);
  23. function emitEvent(uint8 n) public {
  24. emit Hey(n);
  25. }
  26. }
  27. "#;
  28. let ns = parse(case_1);
  29. assert_eq!(ns.diagnostics.count_warnings(), 0);
  30. // Unused event
  31. let case_2 = r#"
  32. event Hey(uint8 n);
  33. contract usedEvent {
  34. event Hey(bool);
  35. event Hello(uint8 n);
  36. function emitEvent(uint8 n) public {
  37. emit Hey(n);
  38. }
  39. }
  40. "#;
  41. let ns = parse(case_2);
  42. let warnings = ns.diagnostics.warnings();
  43. assert_eq!(warnings.len(), 2);
  44. assert_eq!(warnings[0].message, "event 'Hey' has never been emitted");
  45. assert_eq!(warnings[1].message, "event 'Hello' has never been emitted");
  46. // Unused event
  47. let case_2 = r#"
  48. contract F {
  49. event Hey(bool);
  50. event Hello(uint8 n);
  51. }
  52. contract usedEvent is F {
  53. event Hey(uint8 n);
  54. function emitEvent(uint8 n) public {
  55. emit Hey(n);
  56. }
  57. }
  58. "#;
  59. let ns = parse(case_2);
  60. let warnings = ns.diagnostics.warnings();
  61. assert_eq!(warnings.len(), 2);
  62. assert_eq!(warnings[0].message, "event 'Hey' has never been emitted");
  63. assert_eq!(warnings[1].message, "event 'Hello' has never been emitted");
  64. // Unused event
  65. let case_2 = r#"
  66. contract F {
  67. event Hey(uint8 n);
  68. }
  69. contract usedEvent is F {
  70. event Hey(uint8 n);
  71. function emitEvent(uint8 n) public {
  72. // reference event in contract F, so our event decl is not used
  73. emit F.Hey(n);
  74. }
  75. }
  76. "#;
  77. let ns = parse(case_2);
  78. assert_eq!(ns.diagnostics.count_warnings(), 1);
  79. assert_eq!(
  80. ns.diagnostics.first_warning().message,
  81. "event 'Hey' has never been emitted"
  82. );
  83. // make sure we don't complain about interfaces or abstract contracts
  84. let case_3 = r#"
  85. abstract contract F {
  86. event Hey(uint8 n);
  87. }
  88. interface G {
  89. event Hey(uint8 n);
  90. }
  91. "#;
  92. let ns = parse(case_3);
  93. assert_eq!(ns.diagnostics.count_warnings(), 0);
  94. }
  95. #[test]
  96. fn constant_variable() {
  97. let file_2 = r#"
  98. uint32 constant outside = 2;
  99. "#;
  100. let file_1 = r#"
  101. import "test2.sol";
  102. contract Testing {
  103. uint32 test;
  104. uint32 constant cte = 5;
  105. constructor() {
  106. test = outside;
  107. test = cte;
  108. }
  109. function get() public view returns (uint32) {
  110. return test;
  111. }
  112. }
  113. "#;
  114. //Constant properly read
  115. let ns = parse_two_files(file_1, file_2);
  116. assert_eq!(ns.diagnostics.count_warnings(), 0);
  117. let file_1 = r#"
  118. import "test2.sol";
  119. contract Testing {
  120. uint32 test;
  121. uint32 constant cte = 5;
  122. constructor() {
  123. test = 45;
  124. }
  125. function get() public view returns (uint32) {
  126. return test;
  127. }
  128. }
  129. "#;
  130. let ns = parse_two_files(file_1, file_2);
  131. assert_eq!(ns.diagnostics.count_warnings(), 2);
  132. assert!(ns
  133. .diagnostics
  134. .warning_contains("storage variable 'cte' has been assigned, but never read"));
  135. assert!(ns
  136. .diagnostics
  137. .warning_contains("global constant 'outside' has never been used"));
  138. }
  139. #[test]
  140. fn storage_variable() {
  141. let file = r#"
  142. contract Test {
  143. string str = "This is a test";
  144. string str2;
  145. constructor() {
  146. str = "This is another test";
  147. }
  148. }
  149. "#;
  150. let ns = parse(file);
  151. let warnings = ns.diagnostics.warnings();
  152. assert_eq!(warnings.len(), 2);
  153. assert_eq!(
  154. warnings[0].message,
  155. "storage variable 'str' has been assigned, but never read"
  156. );
  157. assert_eq!(
  158. warnings[1].message,
  159. "storage variable 'str2' has never been used"
  160. );
  161. let file = r#"
  162. contract Test {
  163. string str = "This is a test";
  164. string str2;
  165. constructor() {
  166. str = "This is another test";
  167. str2 = str;
  168. }
  169. }
  170. "#;
  171. let ns = parse(file);
  172. assert_eq!(ns.diagnostics.count_warnings(), 1);
  173. assert_eq!(
  174. ns.diagnostics.first_warning().message,
  175. "storage variable 'str2' has been assigned, but never read"
  176. );
  177. let file = r#"
  178. contract Test {
  179. string str = "This is a test";
  180. constructor() {
  181. str = "This is another test";
  182. }
  183. }
  184. contract Test2 is Test {
  185. function get() public view returns (string) {
  186. return str;
  187. }
  188. }
  189. "#;
  190. let ns = parse(file);
  191. assert_eq!(ns.diagnostics.count_warnings(), 0);
  192. }
  193. #[test]
  194. fn state_variable() {
  195. let file = r#"
  196. contract Test {
  197. function get() public pure {
  198. uint32 a = 1;
  199. uint32 b;
  200. b = 1;
  201. uint32 c;
  202. uint32 d;
  203. d = 1;
  204. uint32 e;
  205. e = d*5;
  206. d = e/5;
  207. }
  208. }
  209. "#;
  210. let ns = parse(file);
  211. assert_eq!(ns.diagnostics.count_warnings(), 3);
  212. assert!(ns
  213. .diagnostics
  214. .warning_contains("local variable 'b' has been assigned, but never read"));
  215. assert!(ns
  216. .diagnostics
  217. .warning_contains("local variable 'a' is unused"));
  218. assert!(ns
  219. .diagnostics
  220. .warning_contains("local variable 'c' is unused"));
  221. }
  222. #[test]
  223. fn struct_usage() {
  224. let file = r#"
  225. struct testing {
  226. uint8 it;
  227. bool tf;
  228. }
  229. contract Test {
  230. testing t1;
  231. testing t4;
  232. testing t6;
  233. constructor() {
  234. t1 = testing(8, false);
  235. }
  236. function modify() public returns (uint8) {
  237. testing memory t2;
  238. t2.it = 4;
  239. t4 = testing(1, true);
  240. testing storage t3 = t4;
  241. uint8 k = 2*4/t3.it;
  242. testing t5;
  243. return k;
  244. }
  245. }
  246. "#;
  247. let ns = parse(file);
  248. assert_eq!(ns.diagnostics.count_warnings(), 4);
  249. assert!(ns
  250. .diagnostics
  251. .warning_contains("local variable 't2' has been assigned, but never read"));
  252. assert!(ns
  253. .diagnostics
  254. .warning_contains("storage variable 't1' has been assigned, but never read"));
  255. assert!(ns
  256. .diagnostics
  257. .warning_contains("local variable 't5' is unused"));
  258. assert!(ns
  259. .diagnostics
  260. .warning_contains("storage variable 't6' has never been used"));
  261. }
  262. #[test]
  263. fn subscript() {
  264. let file = r#"
  265. contract Test {
  266. int[] arr1;
  267. int[4] arr2;
  268. int[4] arr3;
  269. bytes byteArr;
  270. uint constant e = 1;
  271. function get() public {
  272. uint8 a = 1;
  273. uint8 b = 2;
  274. arr1[a] = 1;
  275. arr2[a+b] = 2;
  276. uint8 c = 1;
  277. uint8 d = 1;
  278. int[] memory arr4;
  279. arr4[0] = 1;
  280. int[4] storage arr5 = arr3;
  281. arr5[c*d] = 1;
  282. byteArr[e] = 0x05;
  283. }
  284. }
  285. "#;
  286. let ns = parse(file);
  287. assert_eq!(ns.diagnostics.count_warnings(), 4);
  288. assert!(ns
  289. .diagnostics
  290. .warning_contains("local variable 'arr4' has been assigned, but never read"));
  291. assert!(ns
  292. .diagnostics
  293. .warning_contains("storage variable 'arr1' has been assigned, but never read"));
  294. assert!(ns
  295. .diagnostics
  296. .warning_contains("storage variable 'arr2' has been assigned, but never read"));
  297. assert!(ns
  298. .diagnostics
  299. .warning_contains("storage variable 'byteArr' has been assigned, but never read"));
  300. let file = r#"
  301. contract Test {
  302. int[] arr1;
  303. int[4] arr2;
  304. int[4] arr3;
  305. bytes byteArr;
  306. uint constant e = 1;
  307. function get() public {
  308. uint8 a = 1;
  309. uint8 b = 2;
  310. arr1[a] = 1;
  311. arr2[a+b] = 2;
  312. assert(arr1[a] == arr2[b]);
  313. uint8 c = 1;
  314. uint8 d = 1;
  315. int[] memory arr4;
  316. arr4[0] = 1;
  317. int[4] storage arr5 = arr3;
  318. arr5[c*d] = 1;
  319. assert(arr4[c] == arr5[d]);
  320. assert(arr3[c] == arr5[d]);
  321. byteArr[e] = 0x05;
  322. assert(byteArr[e] == byteArr[e]);
  323. }
  324. }
  325. "#;
  326. let ns = parse(file);
  327. assert_eq!(ns.diagnostics.count_warnings(), 0);
  328. }
  329. #[test]
  330. fn assign_trunc_cast() {
  331. //This covers ZeroExt as well
  332. let file = r#"
  333. contract Test {
  334. bytes byteArr;
  335. bytes32 baRR;
  336. function get() public {
  337. string memory s = "Test";
  338. byteArr = bytes(s);
  339. uint16 a = 1;
  340. uint8 b;
  341. b = uint8(a);
  342. uint256 c;
  343. c = b;
  344. bytes32 b32;
  345. bytes memory char = bytes(bytes32(uint(a) * 2 ** (8 * b)));
  346. baRR = bytes32(c);
  347. bytes32 cdr = bytes32(char);
  348. assert(b32 == baRR);
  349. if(b32 != cdr) {
  350. }
  351. }
  352. }
  353. "#;
  354. let ns = parse(file);
  355. assert_eq!(ns.diagnostics.count_warnings(), 1);
  356. assert!(ns
  357. .diagnostics
  358. .warning_contains("storage variable 'byteArr' has been assigned, but never read"));
  359. }
  360. #[test]
  361. fn array_length() {
  362. let file = r#"
  363. contract Test {
  364. int[5] arr1;
  365. int[] arr2;
  366. function get() public view returns (bool) {
  367. int[5] memory arr3;
  368. int[] memory arr4;
  369. bool test = false;
  370. if(arr1.length == arr2.length) {
  371. test = true;
  372. }
  373. else if(arr3.length != arr4.length) {
  374. test = false;
  375. }
  376. return test;
  377. }
  378. }
  379. "#;
  380. let ns = parse(file);
  381. assert_eq!(ns.diagnostics.count_warnings(), 0);
  382. }
  383. #[test]
  384. fn sign_ext_storage_load() {
  385. let file = r#"
  386. contract Test {
  387. bytes a;
  388. function use(bytes memory b) pure public {
  389. assert(b[0] == b[1]);
  390. }
  391. function get() public pure returns (int16 ret) {
  392. use(a);
  393. int8 b = 1;
  394. int16 c = 1;
  395. int16 d;
  396. d = c << b;
  397. ret = d;
  398. }
  399. }
  400. "#;
  401. let ns = parse(file);
  402. assert_eq!(ns.diagnostics.count_warnings(), 0);
  403. }
  404. #[test]
  405. fn statements() {
  406. let file = r#"
  407. contract AddNumbers { function add(uint256 a, uint256 b) external pure returns (uint256 c) {c = b;} }
  408. contract Example {
  409. AddNumbers addContract;
  410. event StringFailure(string stringFailure);
  411. event BytesFailure(bytes bytesFailure);
  412. function exampleFunction(uint256 _a, uint256 _b) public returns (uint256 _c) {
  413. try addContract.add(_a, _b) returns (uint256 _value) {
  414. return (_value);
  415. } catch Error(string memory _err) {
  416. // This may occur if there is an overflow with the two numbers and the `AddNumbers` contract explicitly fails with a `revert()`
  417. emit StringFailure(_err);
  418. } catch (bytes memory _err) {
  419. emit BytesFailure(_err);
  420. }
  421. }
  422. function testFunction() pure public {
  423. int three = 3;
  424. {
  425. int test = 2;
  426. int c = test*3;
  427. while(c != test) {
  428. c -= three;
  429. }
  430. }
  431. int four = 4;
  432. int test = 3;
  433. do {
  434. int ct = 2;
  435. } while(four > test);
  436. }
  437. function bytesToUInt(uint v) public pure returns (uint ret) {
  438. if (v == 0) {
  439. ret = 0;
  440. }
  441. else {
  442. while (v > 0) {
  443. ret = uint(uint(ret) / (2 ** 8));
  444. ret |= uint(((v % 10) + 48) * 2 ** (8 * 31));
  445. v /= 10;
  446. }
  447. }
  448. return ret;
  449. }
  450. function stringToUint(string s) public pure returns (uint result) {
  451. bytes memory b = bytes(s);
  452. uint i;
  453. result = 0;
  454. for (i = 0; i < b.length; i++) {
  455. uint c = uint(b[i]);
  456. if (c >= 48 && c <= 57) {
  457. result = result * 10 + (c - 48);
  458. }
  459. }
  460. }
  461. }
  462. "#;
  463. let ns = parse(file);
  464. assert_eq!(ns.diagnostics.count_warnings(), 2);
  465. assert!(ns
  466. .diagnostics
  467. .warning_contains("function parameter 'a' is unused"));
  468. assert!(ns
  469. .diagnostics
  470. .warning_contains("local variable 'ct' is unused"));
  471. }
  472. #[test]
  473. fn function_call() {
  474. let file = r#"
  475. contract Test1 {
  476. uint32 public a;
  477. constructor(uint32 b) {
  478. a = b;
  479. }
  480. }
  481. contract Test2{
  482. function test(uint32 v1, uint32 v2) private returns (uint32) {
  483. uint32 v = 1;
  484. Test1 t = new Test1(v);
  485. uint32[2] memory vec = [v2, v1];
  486. return vec[0] + t.a();
  487. }
  488. function callTest() public {
  489. uint32 ta = 1;
  490. uint32 tb = 2;
  491. ta = test(ta, tb);
  492. }
  493. }
  494. contract C {
  495. uint public data;
  496. function x() public returns (uint) {
  497. data = 3;
  498. return this.data();
  499. }
  500. }
  501. "#;
  502. let ns = parse(file);
  503. assert_eq!(ns.diagnostics.count_warnings(), 0);
  504. let file = r#"
  505. contract Test1 {
  506. uint32 public a;
  507. constructor(uint32 b) {
  508. a = b;
  509. }
  510. }
  511. contract Test2 is Test1{
  512. constructor(uint32 val) Test1(val) {}
  513. function test(uint32 v1, uint32 v2) private returns (uint32) {
  514. uint32 v = 1;
  515. Test1 t = new Test1(v);
  516. uint32[2] memory vec = [v2, v1];
  517. return vec[0] + t.a();
  518. }
  519. function callTest() public {
  520. uint32 ta = 1;
  521. uint32 tb = 2;
  522. ta = test(ta, tb);
  523. }
  524. }
  525. "#;
  526. let ns = parse(file);
  527. assert_eq!(ns.diagnostics.count_warnings(), 0);
  528. }
  529. #[test]
  530. fn array_push_pop() {
  531. let file = r#"
  532. contract Test1 {
  533. uint32[] vec1;
  534. function testVec() public {
  535. uint32 a = 1;
  536. uint32 b = 2;
  537. uint32[] memory vec2;
  538. vec1.push(a);
  539. vec2.push(b);
  540. }
  541. }
  542. "#;
  543. let ns = parse(file);
  544. assert_eq!(ns.diagnostics.count_warnings(), 2);
  545. assert!(ns
  546. .diagnostics
  547. .warning_contains("local variable 'vec2' has been assigned, but never read"));
  548. assert!(ns
  549. .diagnostics
  550. .warning_contains("storage variable 'vec1' has been assigned, but never read"));
  551. let file = r#"
  552. contract Test1 {
  553. uint32[] vec1;
  554. function testVec() public {
  555. uint32 a = 1;
  556. uint32 b = 2;
  557. uint32[] memory vec2;
  558. vec1.push(a);
  559. vec2.push(b);
  560. vec1.pop();
  561. vec2.pop();
  562. }
  563. }
  564. "#;
  565. let ns = parse(file);
  566. assert_eq!(ns.diagnostics.count_warnings(), 2);
  567. assert!(ns
  568. .diagnostics
  569. .warning_contains("storage variable 'vec1' has been assigned, but never read"));
  570. assert!(ns
  571. .diagnostics
  572. .warning_contains("local variable 'vec2' has been assigned, but never read"));
  573. let file = r#"
  574. contract Test1 {
  575. function test_storage(uint64[] storage arr1, uint128[] storage arr2) private {
  576. arr1.push(32);
  577. arr2.pop();
  578. }
  579. function arg_ptr(uint64[] memory arr1, uint16[] memory arr2) private pure {
  580. arr1.push(422);
  581. arr2.pop();
  582. }
  583. }
  584. "#;
  585. let ns = parse(file);
  586. assert_eq!(ns.diagnostics.count_warnings(), 0);
  587. let file = r#"
  588. contract Test1 {
  589. function test_storage(uint64[] storage arr1) private pure {
  590. }
  591. function arg_ptr(uint64[] memory arr2) private pure {
  592. }
  593. }
  594. "#;
  595. let ns = parse(file);
  596. assert_eq!(ns.diagnostics.count_warnings(), 2);
  597. assert!(ns
  598. .diagnostics
  599. .warning_contains("function parameter 'arr1' is unused"));
  600. assert!(ns
  601. .diagnostics
  602. .warning_contains("function parameter 'arr2' is unused"));
  603. }
  604. #[test]
  605. fn return_variable() {
  606. let file = r#"
  607. contract Test1 {
  608. string testing;
  609. function test1() public pure returns (uint32 ret, string memory ret2) {
  610. return (2, "Testing is fun");
  611. }
  612. function test2() public returns (uint32 hey) {
  613. (uint32 a, string memory t) = test1();
  614. testing = t;
  615. }
  616. }
  617. "#;
  618. let ns = parse(file);
  619. assert_eq!(ns.diagnostics.count_warnings(), 3);
  620. assert!(ns
  621. .diagnostics
  622. .warning_contains("destructure variable 'a' has never been used"));
  623. assert!(ns
  624. .diagnostics
  625. .warning_contains("return variable 'hey' has never been assigned"));
  626. assert!(ns
  627. .diagnostics
  628. .warning_contains("storage variable 'testing' has been assigned, but never read"));
  629. }
  630. #[test]
  631. fn try_catch() {
  632. let file = r#"
  633. contract CalledContract {}
  634. contract TryCatcher {
  635. event SuccessEvent(bool t);
  636. event CatchEvent(bool t);
  637. function execute() public {
  638. try new CalledContract() returns(CalledContract returnedInstance) {
  639. emit SuccessEvent(true);
  640. } catch Error(string memory revertReason) {
  641. emit CatchEvent(true);
  642. } catch (bytes memory returnData) {
  643. emit CatchEvent(false);
  644. }
  645. }
  646. }
  647. "#;
  648. let ns = parse(file);
  649. assert_eq!(ns.diagnostics.count_warnings(), 3);
  650. assert!(ns
  651. .diagnostics
  652. .warning_contains("try-catch error bytes 'returnData' has never been used"));
  653. assert!(ns
  654. .diagnostics
  655. .warning_contains("try-catch returns variable 'returnedInstance' has never been read"));
  656. assert!(ns
  657. .diagnostics
  658. .warning_contains("try-catch error string 'revertReason' has never been used"));
  659. let file = r#"
  660. contract CalledContract {
  661. bool public ok = true;
  662. bool private notOk = false;
  663. }
  664. contract TryCatcher {
  665. event SuccessEvent(bool t);
  666. event CatchEvent(string t);
  667. event CatchBytes(bytes t);
  668. function execute() public {
  669. try new CalledContract() returns(CalledContract returnedInstance) {
  670. // returnedInstance can be used to obtain the address of the newly deployed contract
  671. emit SuccessEvent(returnedInstance.ok());
  672. } catch Error(string memory revertReason) {
  673. emit CatchEvent(revertReason);
  674. } catch (bytes memory returnData) {
  675. emit CatchBytes(returnData);
  676. }
  677. }
  678. }
  679. "#;
  680. let ns = parse(file);
  681. assert_eq!(ns.diagnostics.count_warnings(), 1);
  682. assert!(ns
  683. .diagnostics
  684. .warning_contains("storage variable 'notOk' has been assigned, but never read"));
  685. let file = r#"
  686. contract CalledContract {
  687. bool public ok;
  688. }
  689. "#;
  690. let ns = parse(file);
  691. assert_eq!(ns.diagnostics.count_warnings(), 0);
  692. }
  693. #[test]
  694. fn destructure() {
  695. let file = r#"
  696. contract Test2{
  697. function callTest() public view returns (uint32 ret) {
  698. uint32 ta = 1;
  699. uint32 tb = 2;
  700. uint32 te = 3;
  701. string memory tc = "hey";
  702. bytes memory td = bytes(tc);
  703. address nameReg = address(this);
  704. (bool tf,) = nameReg.call(td);
  705. ta = tf? tb : te;
  706. uint8 tg = 1;
  707. uint8 th = 2;
  708. (tg, th) = (th, tg);
  709. return ta;
  710. }
  711. }
  712. "#;
  713. let ns = parse(file);
  714. assert_eq!(ns.diagnostics.count_warnings(), 0);
  715. }
  716. #[test]
  717. fn struct_initialization() {
  718. let file = r#"
  719. contract Test1{
  720. struct Test2{
  721. uint8 a;
  722. uint8 b;
  723. }
  724. function callTest() public pure returns (uint32 ret) {
  725. uint8 tg = 1;
  726. uint8 th = 2;
  727. Test2 memory t2;
  728. t2 = Test2(tg, th);
  729. ret = t2.a;
  730. }
  731. }
  732. "#;
  733. let ns = parse(file);
  734. assert_eq!(ns.diagnostics.count_warnings(), 0);
  735. }
  736. #[test]
  737. fn subarray_mapping_struct_literal() {
  738. let file = r#"
  739. contract T {
  740. int p;
  741. constructor(int b) {
  742. p = b;
  743. }
  744. function sum(int a, int b) virtual public returns (int){
  745. uint8 v1 = 1;
  746. uint8 v2 = 2;
  747. uint8 v3 = 3;
  748. uint8 v4 = 4;
  749. uint8[2][2] memory v = [[v1, v2], [v3, v4]];
  750. return a + b * p/v[0][1];
  751. }
  752. }
  753. contract Test is T(2){
  754. struct fooStruct {
  755. int foo;
  756. int figther;
  757. }
  758. mapping(string => int) public mp;
  759. enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
  760. FreshJuiceSize choice;
  761. function sum(int a, int b) override public returns (int) {
  762. choice = FreshJuiceSize.LARGE;
  763. return a*b;
  764. }
  765. function test() public returns (int){
  766. int a = 1;
  767. int b = 2;
  768. int c = super.sum(a, b);
  769. int d = 3;
  770. fooStruct memory myStruct = fooStruct({foo: c, figther: d});
  771. string memory t = "Do some tests";
  772. mp[t] = myStruct.figther;
  773. return mp[t];
  774. }
  775. }
  776. "#;
  777. let ns = parse(file);
  778. assert_eq!(ns.diagnostics.count_warnings(), 1);
  779. assert!(ns
  780. .diagnostics
  781. .warning_contains("storage variable 'choice' has been assigned, but never read"));
  782. }
  783. #[test]
  784. fn builtin_call_destructure() {
  785. let file = r#"
  786. contract Test {
  787. function test() public returns(bool p) {
  788. uint128 b = 1;
  789. uint64 g = 2;
  790. address payable ad = payable(address(this));
  791. bytes memory by = hex"AB2";
  792. (p, ) = ad.call{value: b, gas: g}(by);
  793. uint c = 1;
  794. abi.encodeWithSignature("hey", c);
  795. uint128 amount = 2;
  796. ad.send(amount);
  797. uint128 amount2 = 1;
  798. ad.transfer(amount2);
  799. }
  800. }
  801. "#;
  802. let ns = parse(file);
  803. assert_eq!(ns.diagnostics.count_warnings(), 0);
  804. }
  805. #[test]
  806. fn delete_statement() {
  807. let file = r#"
  808. pragma solidity 0;
  809. contract Test1{
  810. int test8var;
  811. function test8() public {
  812. delete test8var;
  813. test8var = 2;
  814. }
  815. }
  816. "#;
  817. let ns = parse(file);
  818. assert_eq!(ns.diagnostics.count_warnings(), 0);
  819. }
  820. #[test]
  821. fn load_length() {
  822. let file = r#"
  823. contract foo {
  824. function f(uint i1) public pure returns (int) {
  825. int[8] bar = [ int(10), 20, 30, 4, 5, 6, 7, 8 ];
  826. bar[2] = 0x7_f;
  827. return bar[i1];
  828. }
  829. function barfunc() public pure returns (uint) {
  830. uint[2][3][4] array;
  831. return array.length;
  832. }
  833. }
  834. "#;
  835. let ns = parse(file);
  836. assert_eq!(ns.diagnostics.count_warnings(), 0);
  837. }
  838. #[test]
  839. fn address_selector() {
  840. let file = r#"
  841. contract ctc {
  842. function foo(int32 a) public pure returns (bool) {
  843. return a==1;
  844. }
  845. function test() public view {
  846. function(int32) external returns (bool) func = this.foo;
  847. assert(address(this) == func.address);
  848. assert(func.selector == hex"42761137");
  849. }
  850. }
  851. "#;
  852. let ns = parse(file);
  853. assert_eq!(ns.diagnostics.count_warnings(), 0);
  854. }
  855. #[test]
  856. fn load_storage_load() {
  857. let file = r#"
  858. struct X {
  859. uint32 f1;
  860. bool f2;
  861. }
  862. contract foo {
  863. function get() public pure returns (X[4] f) {
  864. f[1].f1 = 102;
  865. f[1].f2 = true;
  866. }
  867. }
  868. "#;
  869. let ns = parse(file);
  870. assert_eq!(ns.diagnostics.count_warnings(), 0);
  871. }
  872. #[test]
  873. fn variable_function() {
  874. let file = r#"
  875. contract ft is Arith {
  876. function test(bool action, int32 a, int32 b) public returns (int32) {
  877. function(int32,int32) internal returns (int32) func;
  878. if (action) {
  879. func = Arith.mul;
  880. } else {
  881. func = Arith.add;
  882. }
  883. return func(a, b);
  884. }
  885. }
  886. contract Arith {
  887. function mul(int32 a, int32 b) internal pure returns (int32) {
  888. return a * b;
  889. }
  890. function add(int32 a, int32 b) internal pure returns (int32) {
  891. return a + b;
  892. }
  893. }
  894. "#;
  895. let ns = parse(file);
  896. assert_eq!(ns.diagnostics.count_warnings(), 0);
  897. let file = r#"
  898. contract ft {
  899. function test() public {
  900. function(int32) external returns (uint64) func = this.foo;
  901. assert(func(102) == 0xabbaabba);
  902. }
  903. function foo(int32) public pure returns (uint64) {
  904. return 0xabbaabba;
  905. }
  906. }
  907. "#;
  908. let ns = parse(file);
  909. assert_eq!(ns.diagnostics.count_warnings(), 0);
  910. let file = r#"
  911. contract ft {
  912. function(int32,int32) internal returns (int32) func;
  913. function mul(int32 a, int32 b) internal pure returns (int32) {
  914. return a * b;
  915. }
  916. function add(int32 a, int32 b) internal pure returns (int32) {
  917. return a + b;
  918. }
  919. function set_op(bool action) public {
  920. if (action) {
  921. func = mul;
  922. } else {
  923. func = add;
  924. }
  925. }
  926. function test(int32 a, int32 b) public returns (int32) {
  927. return func(a, b);
  928. }
  929. }
  930. "#;
  931. let ns = parse(file);
  932. assert_eq!(ns.diagnostics.count_warnings(), 0);
  933. let file = r#"
  934. contract ft {
  935. function mul(int32 a, int32 b) internal pure returns (int32) {
  936. return a * b;
  937. }
  938. function add(int32 a, int32 b) internal pure returns (int32) {
  939. return a + b;
  940. }
  941. function test(bool action, int32 a, int32 b) public returns (int32) {
  942. function(int32,int32) internal returns (int32) func;
  943. if (action) {
  944. func = mul;
  945. } else {
  946. func = add;
  947. }
  948. return func(a, b);
  949. }
  950. }
  951. "#;
  952. let ns = parse(file);
  953. assert_eq!(ns.diagnostics.count_warnings(), 0);
  954. let file = r#"
  955. contract ft is Arith {
  956. function mul(int32 a, int32 b) internal pure override returns (int32) {
  957. return a * b * 10;
  958. }
  959. function add(int32 a, int32 b) internal pure override returns (int32) {
  960. return a + b + 10;
  961. }
  962. }
  963. contract Arith {
  964. function test(bool action, int32 a, int32 b) public returns (int32) {
  965. function(int32,int32) internal returns (int32) func;
  966. if (action) {
  967. func = mul;
  968. } else {
  969. func = add;
  970. }
  971. return func(a, b);
  972. }
  973. function mul(int32 a, int32 b) internal virtual returns (int32) {
  974. return a * b;
  975. }
  976. function add(int32 a, int32 b) internal virtual returns (int32) {
  977. return a + b;
  978. }
  979. }
  980. "#;
  981. let ns = parse(file);
  982. assert_eq!(ns.diagnostics.count_warnings(), 0);
  983. let file = r#"
  984. function global_function() pure returns (uint32) {
  985. return 102;
  986. }
  987. function global_function2() pure returns (uint32) {
  988. return global_function() + 5;
  989. }
  990. contract c {
  991. function test() public {
  992. function() internal returns (uint32) ftype = global_function2;
  993. uint64 x = ftype();
  994. assert(x == 107);
  995. }
  996. }
  997. "#;
  998. let ns = parse(file);
  999. assert_eq!(ns.diagnostics.count_warnings(), 0);
  1000. }
  1001. #[test]
  1002. fn format_string() {
  1003. let file = r#"
  1004. contract foo {
  1005. constructor() {
  1006. int x = 21847450052839212624230656502990235142567050104912751880812823948662932355201;
  1007. print("x = {}".format(x));
  1008. }
  1009. }
  1010. "#;
  1011. let ns = parse(file);
  1012. assert_eq!(ns.diagnostics.count_warnings(), 0);
  1013. }
  1014. #[test]
  1015. fn balance() {
  1016. let file = r#"
  1017. contract foo {
  1018. function test(address payable addr) public pure returns (bool) {
  1019. bool p;
  1020. p = addr.balance == 2;
  1021. return p;
  1022. }
  1023. }
  1024. "#;
  1025. let ns = parse(file);
  1026. assert_eq!(ns.diagnostics.count_warnings(), 0);
  1027. }